Create Object in C++


There are two different methods for creating objects of the class:
(1) We can create object at the time of specifying a class after the closing curly bracket.

Example:
Class test
{
int a,b;
public:
void input ();
void ouput ();
}t1,t2,t3;
Here, t1, t2 and t3 are the objects of class test.

(2) We can create object inside the main function using name of the class.
The general syntax for creating object inside main function is as below:
Class_Name Object_Name;
Example:
Test t1, t2, t3;

Here, t1, t2 and t3 are the objects of class test.

     

How to Access Class Members?


The data member and member function declared as a public can be accessed directly using the object of the class. But the data member and member function declared as private can not be accessed directly using the object of the class.
We can access private member of the class using public member of the class.
The general syntax for accessing public member using object is given below:
Object_Name. Data_Member = value;
Object_Name. Member_Function (Argument_List);
Example:
T1. input ();
T1. output ();

Example:
class Test
{
int b;
Public:
int a;
void inputb()
{
b=20;
}
};
int main()
{
Test t1;
T1.a=10; //works because a is public
T1.b=20; //error because b is private
T1.inputb (); //works because inputb () is public
return 0;
}

Download Projects


Download Programs