A Program to understand concepts of 'Class & Object' :-


(a) to create a class.
(b) to create objects of that class.
(c) to assign value into data members using objects.
(d) to display the data entered using objects.

#include<iostream.h>
#include<string.h>
#include<conio.h>
class employee
{
public:
int eid;
char name[3];
};
void main()
{
clrscr();
employee e1,e2;
e1.eid=1;
strcpy(e1.name,"ABC");
e2.eid=2;
strcpy(e2.name,"XYZ");
cout<<e1.eid<<"__"<<e1.name<<endl;
cout<<e2.eid<<"__"<<e2.name;
getch();
}

Output:


1__ABC
2__XYZ

			


C++ Classes and Objects :-


C++ is a multi-paradigm programming language. Meaning, it supports different programming styles.
One of the popular ways to solve a programming problem is by creating objects, known as object-oriented style of programming.
C++ supports object-oriented (OO) style of programming which allows you to divide complex problems into smaller sets by creating objects.
Object is simply a collection of data and functions that act on those data.




C++ Class :-


Before you create an object in C++, you need to define a class.
A class is a blueprint for the object.
We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object.
As, many houses can be made from the same description, we can create many objects from a class.



C++ Objects :-


When class is defined, only the specification for the object is defined; no memory or storage is allocated.
To use the data and access functions defined in the class, you need to create objects.

Previous Next