Limitation of Structures
The standard C does not allow the struct data type to be treated as built-in type.
Struct mynum m1, m2, m3;
But m3=m1+m2; is illegal in C.
Another limitation of C structures is that they do not permit data hiding, structure members are public and can be accessed by any function within the scope.
C++ defines a new user defined data type is called Class. A class supports all object oriented programming concept like inheritance.etc.
In C++, the only difference between class and a structure is by default the members of a class are private and by default members of structure are public.
A class is an abstract data type and can be treated as any built-in data type. A class contains data members and member functions which are meant to operate on data members.
Here, keywords private and public are known as visibility labels.
1. Usage of keyword private is optional by default, all the members are private.
2. Only member functions can have access to the private data members and private functions. However, public members can be accessed from outside the class using objects of the same class.
3. A variable of a class is called object. Every class has it's object. Object gets memory.
4. A member function can be defined in two places
a. Outside class definition
b. Inside class definition
Program to add two numbers using class:
float a, b, result;
Void getdata(float x, float y);
Void myadd::getdata(float x, float y)
Nesting of member function
A member function can be called within another member function of the same class without using object.
int a, b;
void getnum(int x, int y);
void largestnum::getnum(int x, int y)
Note:- Private member functions cannot be accessed my the object, they can only be called by other member functions of the same class.