0% found this document useful (0 votes)
15 views9 pages

Practice Inheritance

Uploaded by

sachinkj143143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views9 pages

Practice Inheritance

Uploaded by

sachinkj143143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

//Inheritance implementation using general class(class names: A, B etc.

,)
definitions

#include <iostream>
using namespace std;
//1. Implementing inheritance: public
//Base class
/*
class A{
public:
int a;
void printData(){
cout<<"a:"<<a<<endl;
}
};
//Derived class
class B:public A{
//no data and functions
//This class is inheriting A
};
int main(){
B obj; //create sub class object
obj.a=10; //a is data member of base class
obj.printData(); //The function printData() inherited
//from base class A
//cout<<obj.a<<endl;
return 0;
}
*/
/////////////////////
//2. Access specifiers in inheritance
//Base class
/*
class A{
private:
int privateA;
protected:
int protectedA;
public:
int publicA;
};
//subclass inheriting from A
class B: public A{

};
int main(){
A obj; //base class object
//obj.privateA=10; //not accessible
//obj.protectedA=10; //not accessible
obj.publicA=10; //accessible
cout<<obj.publicA<<endl;
B obj1;
//obj1.privateA=10; //not accessible
//obj1.protectedA=10; //not accessible
obj1.publicA=11; //accessible
cout<<obj1.publicA<<endl;
return 0;
}
*/
//////////////////////////////////////////////////
//2. Access specifiers in inheritance
//Base class
/*
class A{
private:
int privateA;
protected:
int protectedA;
public:
int publicA;
};
//subclass inheriting from A
class B: public A{

};
int main(){
A obj; //base class object
//obj.privateA=10; //not accessible
//obj.protectedA=10; //not accessible
obj.publicA=10; //accessible
cout<<obj.publicA<<endl;
B obj1;
//obj1.privateA=10; //not accessible
//obj1.protectedA=10; //not accessible
obj1.publicA=11; //accessible
cout<<obj1.publicA<<endl;
return 0;
}
*/
*****************************************************
//3. How to access protected data
/*
class A{
protected:
int protectedA;
public:
//Like for private members, we need to write set and get functions
//to access protected members from base class objects,applicable for
//functions also.
void setA(int a){
protectedA=a;
}
int getA(){
return protectedA;
}
};
int main(){
A obj;
obj.setA(1); //initializing the protected data member
cout<<obj.getA()<<endl; //prints protected member
return 0;
}
*/
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""
//4. Access protected data from sub class
/*
class A{
protected:
int protectedA;
};
class B: public A{
public:
//The subclass is authorized to access and use
//the protected data member of its base class
void setData(int a){
protectedA=a; //initialize acn be in constructor also.
}
int getData(){
return protectedA;
}
};
int main(){
B obj;
obj.setData(11);
cout<<obj.getData()<<endl;
return 0;
}
*/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//The access of protected data member is true throughout the class
hierarchy
/*
class A{
protected:
int protectedA;
};
class B: public A{
public:
void setData(int a){
protectedA=a;
}
int getData(){
return protectedA;
}
};
class C: public B{
public:
void setData(int a){
protectedA=a;
}
int getData(){
return protectedA;
}

};
int main(){
C obj;
obj.setData(10);
cout<<obj.getData()<<endl;
return 0;
}
*/
###############################################################
//5. constructor in inheritance
/*
class A{
private:
int a;
public:
A():a(0){
cout<<"Default constructor:A"<<endl;
}
A(int a):a(a){
cout<<"Parameterized constructor:A"<<endl;
}
};
class B: public A{
public:
B():A(){ //The call to A() reuses the definition of
//base class A(), it is true in parameterized
//constructors also
cout<<"Default constructor:B"<<endl;
}
B(int a):A(a){
cout<<"Parameterized constructor:B"<<endl;
}
};
int main(){
//create base class object
A obj;
//create sub class object: observe the output
//do inference
B obj1;
A obj2(10);
B obj3(11);

return 0;
}
*/
************************************************************
//The sub class is having a data member, but it is not defined in
previous class
//6. Constructor
/*
class A{
private:

public:
int a;
A():a(0){
cout<<"Default constructor:A"<<endl;
}
A(int a):a(a){
cout<<"Parameterized constructor:A"<<endl;
}
};
class B: public A{
public:
int b;
B():A(),b(0){
cout<<"Default constructor:B"<<endl;
}
B(int a,int b):A(a),b(b){
cout<<"Parameterized constructor:B"<<endl;
}
void printData(){
cout<<a<<":"<<b<<endl;
}
};
int main(){
A obj;
A obj1(1);
B obj2;
B obj3(2,3);
obj3.printData();
return 0;
}
*/
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//class hierarchy: CASE-STUDY:Person-Teacher-Student-Course


//Refer the preTest document: The class hierarchy discussion can be found
in that document.
class Person{
public:
string fName;
string lName;
Person(){
this->fName="";
this->lName="";
}
Person(string fName, string lName){
this->fName=fName;
this->lName=lName;
}
void printPerson(){
cout<<fName<<":"<<lName<<endl;
}
};
class Teacher:public Person
{public:
double salary;
Teacher():Person(){
salary=0;
}
Teacher(string fName, string lName,
double salary):Person(fName,lName),salary(salary){
}
void printPerson(){
Person::printPerson();
cout<<salary<<endl;
}
};
class Course{
public:
int courseCode;
string courseTitle;
char grade;
int isa1;
int isa2;
int activity;
//Person *teacher;
Teacher *teacher;
Course(){
this->courseCode=0;
this->courseTitle="";
}
Course(int courseCode, string courseTitle,int isa1, int isa2,int
activity){
this->courseCode=courseCode;
this->courseTitle=courseTitle;
this->isa1=isa1;
this->isa2=isa2;
this->activity=activity;
}
void printCourse(){
cout<<courseCode<<":"<<courseTitle<<":"<<teacher->fName<<":"<<
isa1<<isa2<<activity<<grade<<endl;
}
};
class Student: public Person{
public:
int rollNo;
int sem;
Course *courses;
Student():Person(){
this->rollNo=0;
this->sem=0;
}
Student(string fName,string lName,
int rollNo,int sem):Person(fName,lName){
Person::printPerson();
this->rollNo=rollNo;
this->sem=sem;
}
void printStudent(){
int i;

cout<<rollNo<<":"<<sem<<endl;
for(i=0;i<3;i++){
courses[i].printCourse();
}
}
void enrollCourse(){
int i,n;
cout<<"No of courses"<<endl;
cin>>n;
for(i=0;i<n;i++){
//cout<<"Enter course Details"<<endl;
cout<<"Enter code, title,teachername,
marks:isa1,isa2,activity"<<endl;

cin>>courses[i].courseCode>>courses[i].courseTitle>>courses[i].teacher-
>fName>>courses[i].isa1>>courses[i].isa2>>courses[i].activity;
}
}
void computeGrade(){
int sum,i;
for(i=0;i<3;i++){
sum=courses[i].isa1+courses[i].isa2+courses[i].activity;
if(sum>45){
courses[i].grade='S';}
else if(sum<45 && sum>=35)
{courses[i].grade='A';}
else
{courses[i].grade='B';}
}
}
};
int main(){
//Create courses
Course courses1[3]={Course(101,"xyz",10,10,8),
Course(102,"aaa",18,15,9),Course(103,"asdf",15,15,7)};
//Assign Teacher for the course
courses1[0].teacher=new Teacher("s","aa",50000);
courses1[1].teacher=new Teacher("t","bb",50000);
courses1[2].teacher=new Teacher("g","cc",50000);
Student s1("RAM","Charan",524,4);
//Assign courses to student
s1.courses=courses1;
s1.computeGrade();
s1.printStudent();
//Student enroll for courses
s1.enrollCourse();
s1.computeGrade();
s1.printStudent();
return 0;
}
////////////////////////////////////////////////////////////////////
//static in inheritance: The static data member of base class is
available throughout the class
//heirarchy
/*
class A{
public:
int a;
static int objCount;
A(){
objCount++;
}
A(int a){
this->a=a;
}
};
int A::objCount=0;

class B: public A{
public:
int b;
B():A()
{
this->b=0;
}
B(int a, int b):A(a){
this->b=b;
}
};
int main(){
A obj;
B obj1,obj2;
cout<<obj1.objCount<<endl;
return 0;
}
/////////////////////////////////////////////////////////////////////////
//constructor: Another method: Using "this operator"
/*
class A{
public:
int a;
A(){
cout<<"Test"<<endl;
this->a=0;
}
A(int a){
cout<<"PAram"<<endl;
this->a=a;
}
};
class B:public A{
public:
int b;
B(){
this->a=a;
}
B(int a, int b){
this->a=a;
this->b=b;
}
};
int main(){
B obj;
B obj1(1,1);
return 0;
}
////////////////////////////////////////////////////
//Method overriding: polymorphism
//Write a c++ program to define a class A with a data member,
//add a function to increment the data member.Write another class
//B with a data member and override the function of A to increment
//B's data member.
class A{
public:
int a;
A(){
this->a=0;
}
A(int a){
this->a=a;
}
void incrementData(int x){ //Base class function
this->a+=x;
}
void printData(){ //Base class function
cout<<"a:"<<a<<endl;
}
};
class B:public A{
public:
int b;
B():A(){
this->b=0;
}
B(int a, int b):A(a){
this->b=b;
}
void incrementData(int x){ //Derived class function having the same
function signature/prototype
//of base class: It is called function overriding(polymorphism)
this->b+=x;
A::incrementData(x);
}
void printData(){//Derived class function having the same function
signature/prototype
//of base class: It is called function overriding(polymorphism)
A::printData();
cout<<"b:"<<b<<endl;
}
};
int main(){
A obj(10);
obj.incrementData(1);
obj.printData();
B obj1(9,10);
obj1.printData();
obj1.incrementData(1);
obj1.printData();
//A=B
obj=obj1; //Assigning sub class object to base class object
obj.printData();//print A's data, because the base class is not having
the knowledge
//of sub class.
return 0;
}
/////////////////////////////////////////////////////////////

You might also like