class employee
{
public:
int empid;
string empname;
float salary;
};
Consider the above Employee class. Object for Employee class can be defined as:
Employee e;
Here, object e of Employee class is defined.
#include <iostream>
using namespace std;
class Employee
{
private:
int empid;
string empname;
float salary;
public:
int emp_details()
{
empid=100;
empname="ABC";
salary=10000.0;
}
int show()
{
cout<<"Employee Id : "<<empid<<endl;
cout<<"Employee Name : "<<empname<<endl;
cout<<"Employee Salary : "<<salary<<endl;
}
};
int main()
{
Employee e;
e.emp_details();
e.show();
return 0;
}
Class | Structure |
---|---|
Class is a reference type. | Structure is a value type. |
In class, object is created on the heap memory. | In structure, object is created on the stack memory. |
It supports inheritance. | It does not support inheritance. |
It includes all types of constructors and destructors. | It includes only parameterized constructors. |
Object can be created using new keyword. For eg. Test t = new Test(); | Object can be created without using the new keyword. For eg. Test t; |
The member variable of class can be initialized directly. | The member variable of structure cannot be initialized directly. |