Class and Object in C++ and a simple program to understand it.

Class definition in c++ :
Class is defined  in c++ using class statement. Inside the class we defined the member variable of the class and the function of the class.Special constructor function is used for creating a object of class. Class are very necessary in C++  which lead c++ as a object oriented programming language.
Simple program on Class and Object in C++.

#include
#include
#include
class Book
{
char name[80],author[80];
int price;
public:Book(char n[80],char a[80],int p)
{
strcpy(name,n);
strcpy(author,a);
price=p;
}
public:void display()
{
cout<<"\nName= "<<name<<",Author ="<<author<<",Price= "<<price;
}
public: ~Book()
{
cout<display();
delete(b2);
getch();
}

Program will tell you a lot about defining Class and Object making constructor and other stuff. Be sure that we define class above the main function and we just call them in the main function . Here is syntax:-

class name_of_class{
variable declaration
constructor declaration 
};

Be sure that constructor has no return type it will always void .

Leave a Reply