by Dinesh Thakur Category: Classes in C++

The difference between a structure and a class is that, in a class, the member data or functions are private by default whereas, in a structure, they are public by default. The following segment

 

class Product

{

private:

char name[10];

int price;

public:

void input();

{

cin >> name;

cin>>price;

}

};

 

is equivalent to

 

class Product

{

char name[10];

int price;

public:

void input()

{

cin>> name;

cin>>price;

}

};

since in a class, the members are private by default.

In a structure, the members are public by default. Hence if Product is declared as a structure, the keyword private has to be mentioned in case the data is private.

However, the word public need not be mentioned before the function input() since it is public by default. This is illustrated below:

struct Product

{

private:

char name[10];

char colour[5];

void input()

{

cin>>name;

cin>>colour;

}

}



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles