Category Archives: Inheritance

Inheritance in C++ – Part 6

In this video I have explained about Inheritance in C++:

1. Explain Inheritance.

2. Explain the use of “using” keyword in Inheritance.

3. Example of showing the use of “using” keyword to change the access specifer.

Secondly I have shown code explaining the use of “using” keyword in C++ Inheritance.


/*
Developed By: Prof. Vinod Pillai
vinodthebest@gmail.com
Inheritance - Using keyword.
*/

#include

using namespace std;

class First
{
private:

int prival;

protected:

int proval;

public:

int pubval;

void set(int tprival,int tproval,int tpubval)
{
prival=tprival;
proval=tproval;
pubval=tpubval;
}
void display()
{
cout<<"\n The Private is:"<<prival;
cout<<"\n The Protected is:"<<proval;
cout<<"\n The Public is:"<<pubval;
}
};

class Second : private First
{
int result;

public:

//Using keyword to convert private data to public

using First::pubval;

void set()
{
First::set(10,20,30);
}
void display()
{
First::display();
}
};

class Third:public Second
{

};

int main()
{
Second sobj;

sobj.pubval=500;

cout<<"\nThe public variable value is:"<<sobj.pubval;

Third tobj;

tobj.pubval=200;

cout<<"\nThe public variable value is:"<<tobj.pubval;

return 0;
}

Advertisements

2 Comments

Filed under C++, Inheritance

Inheritance in C++ Containership – Part 5

In this video I have explained about Inheritance & Containership in C++:

1. Explain Inheritance.

2. Two major ways of accessing data in C++:

1. Inheritance.

2. Containership or nesting.

3. When to use Inheritance and when to use containership concept.

4. Example showing the use of Containership in C++.


/*
Developed By: Prof. Vinod Pillai
vinodthebest@gmail.com
Inheritance-Containership.
*/

#include

using namespace std;

class Department
{
protected:

int id;
char name[50];

public:

void setDepartment()
{
cout<>id;
cout<>name;
}
void displayDepartment()
{
cout<<"\n Department ID is:"<<id<<"\n";
cout<<"\n Department Name is:"<<name<<"\n";
}
};

class Employee
{
protected:

int eid;
char ename[50];
Department dobj;

public:

void setEmployee()
{
cout<>eid;
cout<>ename;
dobj.setDepartment();
}
void displayEmployee()
{
cout<<"\n Employee ID is:"<<eid<<"\n";
cout<<"\n Employee Name is:"<<ename<<"\n";
dobj.displayDepartment();
}
};

int main()
{
Employee obj;

obj.setEmployee();
obj.displayEmployee();

return 0;
}

1 Comment

Filed under C++, Inheritance

Inheritance in C++ Virtual Base Class – Part 4

In this video I have explained about Inheritance in C++:

1. Need of Virtual Base Class.

2. Example showing the use of Virtual Base Class.


/*
Developed By: Prof. Vinod Pillai
vinodthebest@gmail.com
Inheritance-Vitrual Base Class.
*/

#include

using namespace std;

class SuperClass
{
protected:

int val;

public:

void setSuper(int tval)
{
val=tval;
}
void displaySuper()
{
cout<<"\n The SuperClass Class value is:"<<val<<"\n";
}
};

class FirstParent:virtual public SuperClass
{
protected:

int val;

public:

void setFirst(int tval1)
{
val=tval1;
}
void displayFirst()
{
cout<<"\n The FirstParent Class value is:"<<val<<"\n";
}
};

class SecondParent:virtual public SuperClass
{
protected:

int val;

public:

void setSecond(int tval1,int tval2)
{
val=tval1;
}
void displaySecond()
{
cout<<"\n The SecondParent Class value is:"<<val<<"\n";
}
};

class Child:public FirstParent,public SecondParent
{
protected:

int result;

public:

void setChild(int tval1,int tval2,int tval3)
{
SuperClass::val=tval1;
FirstParent::val=tval2;
SecondParent::val=tval3;

result=SecondParent::val+FirstParent::val+SuperClass::val;
}
void displayChild()
{
displaySuper();
displayFirst();
displaySecond();

cout<<"\n The Child Class value is:"<<result<<"\n";
}
};

int main()
{
Child obj;

obj.setChild(1,2,3);
obj.displayChild();

return 0;
}

Leave a comment

Filed under C++, Inheritance

Inheritance in C++ – Part 3

In this video I have explained about Inheritance in C++:

1. Explain Inheritance.

2. Constructor calling in Inheritance.

3. Destructor calling in Inheritance.

4. Need of Default Constructor in Inheritance.

Secondly I have shown code explaining about Constructor & Destructor calling in Inheritance.

Example 1 Constructor & Destructor calling:


/*
Developed By: Prof. Vinod Pillai
vinodthebest@gmail.com
Inheritance-Constructor & Destructor Calling.
*/

#include

using namespace std;

class First
{
protected:

int val;

public:

First(int tval)
{
val=tval;
cout<<"\n First Constructor Calling \n";
}
void display()
{
cout<<"\n The First Class value is:"<<val<<"\n";
}
~First()
{
cout<<"\n First Destructor Calling \n";
}
};

class Second
{
protected:

int val;

public:

Second(int tval)
{
val=tval;
cout<<"\n Second Constructor Calling \n";
}
void display()
{
cout<<"\n The Second Class value is:"<<val<<"\n";
}
~Second()
{
cout<<"\n Second Destructor Calling \n";
}
};

class Third : public Second,public First
{
int result;

public:

Third(int val1,int val2):First(val1),Second(val2)
{
result=First::val+Second::val;
cout<<"\n Third Constructor Calling \n";
}
void display()
{
First::display();
Second::display();
cout<<"\nThe result is :"<<result<<"\n";
}
~Third()
{
cout<<"\n Third Destructor Calling \n";
}
};

int main()
{
{
Third obj(100,200);

obj.display();
}
return 0;
}

Example 2 Constructor & Destructor calling:


/*
Developed By: Prof. Vinod Pillai
vinodthebest@gmail.com
Inheritance.
*/

#include

using namespace std;

class First
{
protected:

int prival;
int proval;
int pubval;

public:

First()
{
prival=0;
proval=0;
pubval=0;
}
First(int tprival,int tproval,int tpubval)
{
prival=tprival;
proval=tproval;
pubval=tpubval;
}
void display()
{
cout<<"\n The Private is:"<<prival;
cout<<"\n The Protected is:"<<proval;
cout<<"\n The Public is:"<<pubval;
}
};

class Second : public First
{
int result;

public:

Second()
{
proval=10;
pubval=20;
result=proval+pubval;
}
void display()
{
First::display();
}
};

int main()
{
Second obj;

obj.display();

return 0;
}

Leave a comment

Filed under C++, Inheritance

Inheritance in C++ – Part 2

In this video I have explained about Inheritance in C++:

1. Explain Inheritance.

2. Types of Inheritance.

a. Single inheritance.

b. Multiple inheritance.

c. Multi level inheritance.

d. Hybrid inheritance.

4. Example for Inheritance.

Secondly I have shown code explaining different types of Inheritance in C++.


/*
Developed By: Prof. Vinod Pillai
vinodthebest@gmail.com
Inheritance.
*/

#include

using namespace std;

class First
{
private:

int prival;

protected:

int proval;

public:

int pubval;

void set(int tprival,int tproval,int tpubval)
{
prival=tprival;
proval=tproval;
pubval=tpubval;
}
void display()
{
cout<<"\n The Private is:"<<prival;
cout<<"\n The Protected is:"<<proval;
cout<<"\n The Public is:"<<pubval;
}
};

class Second : public First
{
int result;

public:

void set()
{
First::set(10,20,30);
}
void display()
{
First::display();
}
};

int main()
{
Second obj;
obj.set();

obj.display();

return 0;
}

Leave a comment

Filed under C++, Inheritance

Inheritance in C++ – Part 1

Inheritance: Inheritance is a mechanism of reusing and extending existing classes without modifying them, thus producing hierarchical relationships between them.

In this video I have explained about Inheritance in C++:

1. Explain Inheritance & its need.

2. Parent access specifer [Private,Protected & Public] & Child can inherit [Private,Protected & Public]

3. Simple rules for Inheritance.

a. Private variables/method will not accessed in the Child Class.

b. Child inhertes Parent Class = Protected [Parent Protected & Public] = Protected.

c. Child inhertes Parent Class = Private [Parent Protected & Public] = Private.

d. Child inhertes Parent Class = Public [Parent Protected & Public] = [Protected & public].

4. Simple example for Inheritance.

Secondly I have shown code explaining how you can create Inheritance in C++.


/*
Developed By: Prof. Vinod Pillai
vinodthebest@gmail.com
Inheritance.
*/

#include

using namespace std;

class First
{
private:

int prival;

protected:

int proval;

public:

int pubval;

};

class Second : public First
{
int result;

public:

void set()
{
proval=100;
pubval=200;
result=proval+pubval;
}
void display()
{
cout<<"\n The Result is:"<<result<<"\n";
}
};

int main()
{
Second obj;
obj.set();
obj.proval=500;
cout<<"\n The pubval is:"<<obj.proval;
obj.display();

return 0;
}

2 Comments

Filed under C++, Inheritance