class and objects in c++

class is a blueprint or skelton or collection of objects.
 

Class syntax in c++

 
class Class_name{
private:
private_members

public:
public_members

}
 

Object

Object is an real world entity which have properties and behaviours.
 

Create object in c++

 
Class_name object_name;
 
example
 
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;

class Person{

public:
int age;
string name;
};

int main(){

Person p;

cout<<"Enter your name";
cin>>p.name;
cout<<"Enter age:";
cin>>p.age;

cout<<"Person details:"<<endl;
cout<<"Name:"<<p.name<<endl;
cout<<"Age:"<<p.age<<endl;

getch();
return 0;
}
 

Defining member function.

 

Inside a class

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class Person{

public:
int age;
string name;
void display(){
cout<<"age:"<<age<<endl;
cout<<"name:"<<name<<endl;
}
};

int main(){

Person p;
cout<<"Enter your name";
cin>>p.name;

cout<<"Enter age:";
cin>>p.age;

cout<<"Person details:"<<endl;
cout<<"Name:"<<p.name<<endl;
cout<<"Age:"<<p.age<<endl;


getch();
return 0;
}
 
 

Outside a class

 
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class Person{

public:
int age;
string name;
void getData();
};

void Person::getData(){
cout<<"Enter person name:"<<endl;
cin>>name;
cout<<"Enter age:"<<endl;
cin>>age;
}

int main(){
Person p;
cout<<"Enter your name";
cin>>p.name;
cout<<"Enter age:";
cin>>p.age;

cout<<"Person details:"<<endl;
cout<<"Name:"<<p.name<<endl;
cout<<"Age:"<<p.age<<endl;

p.getData();

cout<<"Person details:"<<endl;
cout<<"Name:"<<p.name<<endl;
cout<<"Age:"<<p.age<<endl;

getch();
return 0;
}
 

Constructor

It is a special member function of a class, it has task to initialize the object of a class, it is a special because it has name same as class name, it is called automatically when an object of a class created, constructor does not have any kind of data type, and it does not return a value.
 
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;

class Person{

public:
int age;
string name;
Person(){
age=0;
name="Rama";
}
};

int main(){
Person p;
cout<<p.name<<endl;
cout<<p.age;

getch();
return 0;
}


Destructor

It is used to destroy the object that have be created by the constructor.
Like constructor, destructor has name as class name but it is precceded by the tilde(~).
 
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;

class Person{
public:
int age;
string name;

Person(){
age=0;
name="Rama";
}

~Person(){
cout<<"destructor is called";
}
};

int main(){

Person p;
cout<<p.name<<endl;
cout<<p.age;
{
Person q;
}

getch();
return 0;
}
 
 

Friend function

Friend function is used to access private member of a class using non member function of a class.
 
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class Person{
int a=10;
public:
friend void display(Person p);
};

void display(Person p){
cout<<p.a;
}

int main(){
Person p;
display(p);

getch();
return 0;
}
 

This pointer

It is called current class object, we access the member of class directly by the name of member but we can also use this pointer which will be used as current class object.
 
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class Person{
int a=10;
public:
void display();
};

void Person:: display(){
cout<<this->a;
}

int main(){
Person p;
p.display();

getch();
return 0;
}
 

Inheritance

The mechanism of deriving a new class from an old one is called the inheritance.
 

Derived/Sub class

New class that is born from the old one is called derived or sub class.
 

Base class

A class from where new class derived or born is called base class or parent class.
 

In c++, there are 5 types of inheritance.

 
  1. Simple inheritance
  2. Multilevel inheritance
  3. Multiple Inheritance
  4. Hierarchical inheritance
  5. Hybrid Inheritance
 

Simple inheritance

When a class derived from a single class is called the simple inheritance.
 
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class Parent{
public:
int x=100;
};
class Child:public Parent{

};
int main(){
Child c;
cout<<c.x;

getch();
return 0;
}
 
 

Multilevel inheritance

When a class derived from a class and that class derived from the other class is called the multilevel inheritance.
 
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class GrandParent{
public:
int grand_parent_asset=10;
};

class Parent: public GrandParent{
public:
int parent_asset=50;
};

class Child:public Parent{
public:
int child_asset=20;
};

int main(){

Child c;
cout<<c.child_asset<<endl;
cout<<c.parent_asset<<endl;
cout<<c.grand_parent_asset<<endl;

getch();
return 0;
}
 

Multiple Inheritance

When a class derived from two or more base class is called multiple inheritance.
 
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class Father{
public:
int father_asset=10;
};

class Mother{
public:
int mother_asset=50;
};

class Child:public Father, public Mother{
public:
int child_asset=20;
};

int main(){
Child c;
cout<<c.child_asset<<endl;
cout<<c.father_asset<<endl;
cout<<c.mother_asset<<endl;

getch();
return 0;
}
 
 

Hierarchical inheritance

When multiple classes derived from the same class or one class is called hierarchical inheritance.
 
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class Mother{
public:
int mother_asset=10;
};

class Child1:public Mother{
public:
int child1_asset=50;
};

class Child2:public Mother{
public:
int child2_asset=50;
};

int main(){

Child1 c1;
cout<<c1.child1_asset<<endl;
cout<<c1.mother_asset<<endl;

Child2 c2;
cout<<c2.child2_asset<<endl;
cout<<c2.mother_asset<<endl;

getch();
return 0;
}
 

Hybrid inheritance

When more than one inheritance occur in a single situation is called hybrid inheritance.
 
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class GrandFather{
public:
int grand_father_asset=10;
};

class Father:public GrandFather{
public:
int father_asset=50;
};

class Uncle{
public:
int uncle_asset=100;
};

class Child: public Father, public Uncle{
public:
int child_asset=1000;
};

int main(){

Child c;

cout<<"Child has asset:"<<c.child_asset<<endl;
cout<<"Uncle has asset:"<<c.uncle_asset<<endl;
cout<<"Father has asset:"<<c.father_asset<<endl;
cout<<"Grand father has asset:"<<c.grand_father_asset<<endl;

getch();
return 0;
}
© 2016 - 2022, All Rights are Reserved.