Вы находитесь на странице: 1из 90

• Qu.1 What is inheritance?

What is use of
inheritance? Explain. (W17) 4M
Ans:
• The mechanism of deriving new class from an
old/existing one is called inheritance.
OR
• Inheritance is the process by which objects of
one class acquired the properties of objects of
another classes.
Syntax:
class derived-class-name: visibility-mode base-class-name
{
------//
-----// members of derived class
-----//
};
• Use:
• Reusing the properties of the existing class.
The mechanism of deriving a new class from
an old one is called inheritance. The old class
is referred to as the base class and the new
one is called as the derived class or subclass.
Derive class can inherit all or some properties
of its base classes.

• 2 Define derived class. Give one example. (S17) 2M
• Ans:
• Derived class:- A new class derived from an old class is
called as derived class.
• Example:-
• class A
• {
• };
• class B: public A
• {
• };
• In the above example class B is a derived class.
• Explain access specifiers with suitable
example. (S17) 4M
Ans:
Access specifiers :
• 1. private
• 2. protected
• 3. public
• Private access specifier: Class members declared
as private can be accessed only from within the
class. Outside class access is not allowed for
private members of class. By default members
are private.
• Protected access specifier: Class members
declared as protected can be accessed by the
member functions within its class and any class
immediately derived from it. These members
cannot be accessed by the functions outside
these two classes.
• Public access specifier: Class members declared
as public can be accessed from outside the class
also.
EXAMPLE OF ACCESS SPECIFIER
class base
{
private:
int a;
protected:
int b;
public:
void display()
{
cout<<a<<b;
}

};
class derived: public base
{
public:
void show()
{
cout<<b;
}
};
void main()
{
derived d;
d.display();
d.show();
}
SINGLE INHERITANCE
#include<iostream.h>

class AddData //Base Class


{
protected:
int num1, num2;
public:
void accept()
{
cout<<"\n Enter First Number : ";
cin>>num1;
cout<<"\n Enter Second Number : ";
cin>>num2;
}
};
class Addition: public AddData //Class Addition – Derived Class
{
int sum;
public:
void add()
{
sum = num1 + num2;
}
void display()
{
cout<<"\n Addition of Two Numbers : "<<sum;
}
};
int main()
{
Addition a;
a.accept();
a.add();
a.display();
return 0;
}
Output:
Q.Write a program to implement single inheritance. Declare base class employee with
Emp_No. and Emp_Name. Declare derived class fitness with height and weight. Accept and
display data for one employee. s-18
#include<iostream.h>
#include<conio.h>
class employee
{

protected:
int emp_no;
char emp_name[10];
public:
void gete()
{
cout<<”enter employee details”;
cin>>emp_no;
cin>>emp_name;
}
void pute()
{
cout<<”employee details are=";
cout<<emp_no<<‟\n‟;
cout<<emp_name;
}
};
class fitness:public employee
{
float height,weight;
public:
void getft()
{
cout<<”enter height and weight”;
cin>>height>>weight;
}
void putft()
{
cout<<”height and weight is=”;
cout<<height<<weight;
}
};
Write a program to implement single inheritance. Declare base
class‘Employee’ with emp_no and emp_name. Declare derived class
‘Fitness’ with height and weight. Accept and display data for one
employee. (S17)4M s-18
Ans:
#include<iostream.h>
#include<conio.h>
class employee
{
protected:

int emp_no;
char emp_name[25];
void getdata()
{
cout<<"\n Enter employee no.";
cin>>emp_no;
cout<<"\n Enter emplyee name";
}
void display()
{
cout<<"\n Employee no. is :"<<emp_no;
cout<<"\n Employee name is:"<<emp_name;
}
};
class fitness:public employee
{
protected:
float height,weight;
public:
void getdata()
{
employee::getdata();
cout<<"\n Enter height:";
cin>>height;
cout<<"\n Enter weight:";
cin>>weight;
}
void main()
{
fitness f;
f.gete();
f.pute();
f.getft();
f.putft();
getch();
}
void display()
{
employee::display();
cout<<"\n height is :"<<height;
cout<<"\n weight is :"<<weight;
}
};
void main()
{
clrscr();
fitness f;
f.getdata();
f.display();
getch();
}
MULTILEVEL EX 1
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student // base class
{
private:
int rl;
char nm[20];
public:
void read();
void display();
};
class marks : public student // dervivec from student
{
protected:
int s1;
int s2;
int s3;
public:
void getmarks();
void putmarks();
};
class result : public marks // derived from marks
{
private:
int t;
float p;
char div[10];
public:
void process();
void printresult();
};
• void student::read()
• {
• cout<<"enter Roll no and Name "<<endl;
• cin>>rl>>nm;
• }
• void student:: display()
• {
• cout <<"Roll NO:"<<rl<<endl;
• cout<<"name : "<<nm<<endl;
• }
• void marks ::getmarks()
• {
• cout<<"enter three subject marks "<<endl;
• cin>>s1>>s2>>s3;
• }
• void marks ::putmarks()
• {
• cout <<"subject 1:"<<s1<<endl;
• cout<<" subject 2 : "<<s2<<endl;
• cout <<"subject 3:"<<s3<<endl;
• }

• void result::process()
• {
• t= s1+s2+s3;
• p = t/3.0;
• p>=60?strcpy(div,"first"):p>=50?strcpy(div, "second"): strcpy(div,"third");
• }

• void result::printresult()
• {
• cout<<"total = "<<t<<endl;
• cout<<"per = "<<p<<endl;
• cout<<"div = "<<div<<endl;
• }
• void main()
• {
• result x;
• clrscr();
• x.read();
• x.getmarks();
• x.process();
• x.display();
• x.putmarks();
• x.printresult();
• getch();
• }
EX 2 MULTILEVEL
Class student
{
char name[20];
int roll_no;
public:
void getdata1()
{
cout<<“enter name and roll_no:”;
cin>>name>>roll_no;
}
void display1()
{
cout<<“Name:”<<name;
cout<<“Rollno:”<<roll_no;
}
};
Class test:public student
{
protected:
int sub1,sub2;
public:
void getdata2()
{
cout<<“Enter marks of two subjects:”;
cin>>sub1>>sub2;
}
void display2()
{
cout<<“marks are:”<<sub1<<sub2;
}
}
Class result:public test
{
int sum;
public:
void total
{
sum=sub1+sub2;
}
void display3()
{
cout<<“Sum:”<<total;
}
};
Void main()
{
Result r;
r.getdata1();
}
Ex2 multilevel
#include<iostream.h>
#include<conio.h>
class base1
{
public:
void show1()
{
cout<<"\nThis is base 1";
}

};
class base2

{
public:
void show2()
{
cout<<"\nThis is base 2";
}

};
class derived: public base1, public base2

{
public:
void display()
{
cout<<"\n Executing from derived";
show1();
show2();
}

};
void main()

{
derived d;
clrscr();
d.display();
getch();

}
Multiple inheritance
#include<iostream.h>
#include<conio.h>
Class student
{
int roll_no;
char name[20];
public:
void getdata()
{
cout<<“enter rollno and name”;
cin>>roll_no>>name;
}
Void display()
{
cout<<nam<<roll_no;
}
};
Class test
{
protected:
int sub1,sub2;
public:
void getdata1()
{
cout<<“enter sub1 and sub2”;
cin>>sub1>>sub2;
}
void display1()
{
cout<<sub1<<sub2;
}
};
Class result:public student,public test
{
int total;
public:
void gettotal()
{
total=sub1+sub2;
cout<<total;
}
}
Void main()
{
result r;
r.getdata();
r.getdata1();
r.display();
r.display1();
r.gettotal();
}
ex 2 Multiple
#include<iostream.h>
#include<conio.h>
Class base1
{
protected:
int x;
public:
void showx()
{
cout<<x;
}
};
class base2
{
protected:
int y;
public:
void showy()
{
cout<<y;
}
};
Class derived:public base1,public base2
{
public:
void set(int i,int j)
{
x=i;
y=j;
}
};
void main()
{
derived ob;
ob.set(10,20);
ob.showx();
ob.showy();
return 0;
}
Ex.3 Multiple Inheritance
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno, m1, m2;
public:
void get()
{
cout << "Enter the Roll no :";
cin>>rno;
cout << "Enter the two marks :";
cin >> m1>>m2;
}
};
class sports
{
protected:
int sport_marks;
public:
void getsportmarks()
{
cout << "\nEnter the sports mark :";
cin>>sport_marks;
}
};
class statement : public student, public sports
{
int tot, avg;
public:
void display()
{
tot = (m1 + m2 + sm);
avg = tot / 3;
cout << "\n\n\tRoll No : " << rno << "\n\tTotal : " << tot;
cout << "\n\tAverage : " << avg;
}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsportmarks();
obj.display();
getch();
}
Hierarchical
#include<iostream.h>
#include<conio.h>

class polygon
{
protected:
int width, height;
public:
void input(int x, int y)
{
width = x;
height = y;
}
};
class rectangle : public polygon
{
public:
int areaR ()
{
return (width * height);
}
};

class triangle : public polygon


{
public:
int areaT ()
{
return (width * height / 2);
}
};
void main ()
{
clrscr();
rectangle rect;
triangle tri;
rect.input(6,8);
tri.input(6,10);
cout <<"Area of Rectangle: "<<rect.areaR()<< endl;
cout <<"Area of Triangle: "<<tri.areaT()<< endl;
getch();
}
Explain hybrid inheritance with example.w-17
Description:
"Hybrid Inheritance" is a method where one or more types of inheritance are
combined together and used. Hybrid Inheritance is combination of Single,
Multiple, Hierarchical and Mutilevel Inheritance. We can use any combination
to form hybrid inheritance only single level inheritance cannot be combined
with multi-level inheritance as it will result into multilevel inheritance.
Above diagram is an example of hybrid inheritance. Class current_acc and
class savings_acc derive common members such as name and acc_no from
class account. Then all the data of account is combined in class
fix_depo_acc.
EX1 virtual class
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll;
public:
void get_number(int a)
{
roll= a;
}
void put_number()
{
cout<<"Roll Number: "<<roll;
}
};
class test : virtual public student
{
protected:
float part1, part2;
public:
void get_marks(float x, float y)
{
part1 = x;
part2 = y;
}
void put_marks()
{
cout<<"Marks Obtained: ";
cout<<"Part1 = "<<part1<<"Part2 = "<<part2;
}
};
class sports
{
protected:
float score;
public:
void get_score(float s)
{
score = s;
}
void put_score()
{
cout<<"Sports Score: "<<score;
}
};
class result : public test, public sports
{
private:
float total;
public:
void display()
{
total = part1 + part2 + score;
put_number();
put_marks();
put_score();
cout<<"Total Score: "<<total;
}
};
void main()
{
clrscr();
result student;
student.get_number(83);
student.get_marks(95,98);
student.get_score(9);
student.display();
getch();
}
EX2
include<iostream.h>
#include<conio.h>
class stu
{
protected:
int rno;
public:
void get_no(int a)
{
rno=a;
}
void put_no
{
Cout<<"Roll no"<<rno<<"\n";
}
};
class test:public stu
{
protected:
float part1,part2;
public:
void get_mark(float x,float y)
{
part1=x; part2=y;
}
void put_marks()
{
cout<<"Marksobtained:"<<"part1="<<part1";
cout<<"Marksobtained:"<<"part2="<<part2"
}
};
class sports
{
protected:
float score;
public:
void getscore(float s)
{
score=s;
}
void putscore
{
cout<<"sports:"<<score<<"\n";
}
};
class result: public test, public sports
{
float total;
public:
void display();
};
void result::display()
{
total=part1+part2+score;
put_no();
put_marks();
putscore();
cout<<"Total Score="<<total<<"\n";
}
void main()
{
clrscr();
result stu;
stu.get_no(123);
stu.get_mark(27.5,33.0);
stu.getscore(6.0);
stu.display();
return 0;
}
program
#include<iostream.h>
#include<conio.h>
class arithmetic
{
protected:
int num1, num2;
public:
void getdata()
{
cout<<"For Addition:";
cout<<"\nEnter the first number: ";
cin>>num1;
cout<<"\nEnter the second number: ";
cin>>num2;
}
};
class plus:public arithmetic
{
protected:
int sum;
public:
void add()
{
sum=num1+num2;
}
};
class minus
{
protected:
int n1,n2,diff;
public:
void sub()
{
cout<<"\nFor Subtraction:";
cout<<"\nEnter the first number: ";
cin>>n1;
cout<<"\nEnter the second number: ";
cin>>n2;
diff=n1-n2;
}
};
class result:public plus, public minus
{
public:
void display()
{
cout<<"\nSum of "<<num1<<" and "<<num2<<"= "<<sum;
cout<<"\nDifference of "<<n1<<" and "<<n2<<"= "<<diff;
}
};
void main()
{
clrscr();
result z;
z.getdata();
z.add();
z.sub();
z.display();
getch();
}
Program on virtual Ex2:
#include<iostream.h>
#include<conio.h>
class account
{
protected:
char acc_name[10];
long ph_no;
};
class savings_acc:virtual public account
{
protected:
int s_acc_no;
};
class current_acc:virtual public account
{
protected:
int c_acc_no;
};
class fix_depo_acc:public savings_acc,current_acc
{
int fix_depo_acc_no;
public:
void getdata()
{
cout<<"\n Enter account holder name : ";
cin>>acc_name;
cout<<"\n Enter phone number : ";
cin>>ph_no;
cout<<"\n Enter savings account number : ";
cin>>s_acc_no;
cout<<"\n Enter current account number : ";
cin>>c_acc_no;
cout<<"\n Enter fixed deposit account number : ";
cin>>fix_depo_acc_no;
}
void putdata()
{
cout<<"\n\n Account holder name : "<<acc_name;
cout<<"\n phone number : "<<ph_no;
cout<<"\n Savings account number : "<<s_acc_no;
cout<<"\n Current account number : "<<c_acc_no;
cout<<"\n Fixed deposit account number : "<<fix_depo_acc_no;
}
};
void main()
{
fix_depo_acc f;
clrscr();
f.getdata();
f.putdata();
getch();
}
Constructor in Derived class
#include<iostream>
class A
{
public:
A()
{
cout << "A's constructor called" ;
}
};
class B
{
public:
B()
{
cout << "B's constructor called" ;
}
};
class C: public B, public A // Note the order
{
public:
C()
{
cout << "C's constructor called" ;
}
};

void main()
{
C c;
return 0;
}
Constructor in derived class ex1
class base
{
int a;
public:
base(int x)
{
a=x;
}
void display()
{
cout<<a;
}
}
Derived class ex 1
Class derived:public base
{
int b;
public:
derived(int x1,int y1):base(x1)
{
b=y1;
}
void display()
{
cout<<“b=“<<b;
}
};
Derived class
Void main()
{
derived d(20,30);
d.display1();
d.display2();
getch();
}
Constructor in derived class ex2
Class alpha
{
int x;
public:
alpha(int a)
{
x=a;
}
void display1()
{
cout<<“x=“<<x;
}
};
Constructor in derived class
Class beta
{
int y;
public:
beta(int b)
{
y=b;
}
void display2()
{
cout<<“y=“<<y;
}
};
Class gamma:public alpha,public beta
{
int z;
public:
gammma(int x1,int y1,int z1):alpha(x1),beta(y1)
{
z=c;
}
void display3()
{
cout<<“z=“<<z;
}
};
void main()
{
clrscr(();
gamma g(10,20,30)
g.display1();
g.display2();
g.display3();
getch();
}
Constructor in derived class ex3
class Base
{
int x;
public:
Base()
{
cout << "Base default constructor";
}
};
class Derived : public Base
{
int y;
public:
Derived()
{
cout << "Derived default constructor";
}
Derived(int i)
{
cout << "Derived parameterized constructor";
}
};
void main()
{
Base b;
Derived d1;
Derived d2(10);
}
example
Virtual base class
Q.What is virtual base class? Explain with example.w-17 4m
Virtual base class:
Consider a situation where all three kinds of inheritance, namely, multilevel,
multiple, hierarchical inheritance, are involved. This illustrated in fig a. the
child has two direct base classes, “parent1” & “parent2” which themselves
have a common base class “grandparent”. The child inherits the traits of
“grandparent” via two separate paths. It can also inherit directly as shown by
broken line. The “grandparent” sometimes referred to as indirect base class.
All the public & protected members of “grandparent” are inherited into “child”
twice, first via “parent1” & again via “parent 2”. This means, “child” would
have duplicate sets of the members inherited from “grandparent. This
introduces ambiguity & should be avoided. The duplication of inherited
members due to these multiple paths can be avoided by making the common
base class as virtual base class while declaring the direct or intermediate base
classes as shown below.
Example:
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>rno;
}
void putnumber()
{
cout<<" Roll No:"<<rno;
}
};
class test: virtual public student
{
public:
int part1,part2;
void getmarks()
{
cout<<"Enter Marks";
cout<<"Part1:";
cin>>part1; cout<<"Part2:";
cin>>part2;
}
void putmarks()
{
cout<<" Marks Obtained";
cout<<" Part1:"<<part1;
cout<<"Part2:"<<part2;
}
};
class sports: public virtual student
{
public:
int score;
void getscore()
{
cout<<"Enter Sports Score:";
cin>>score;
}
void putscore()
{
cout<<" Sports Score is:"<<score;
}
};
class result: public test, public sports
{
int total;
public:
void display()
{
total=part1+part2+score;
putnumber();
putmarks();
putscore();
cout<<"Total Score:"<<total;
}
};
void main()
{
result obj;
clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}
What is abstract class? Give one example of abstract class
An abstract class is a class that is designed only to act as base class. It
is not used to create objects.
An abstract class is used to define an implementation and is intended to be
inherited by child classes.
class base
{
protected:
int a;
public:
void getdata()
{
cin>>a;
}
void display()
{
cout<<a;
}
};
class derived: public base
{
protected:
int b;
public:
void getdata1()
{
getdata();
cin>>b;
}
void display1()
{
display();
cout<<b;
}
};
void main()
{
derived d;
d.getdata1();
d.display1();
}
• In the above example, class base is an abstract class since its object is
• not created in main().Its members are accessed through its derived
• class.
Virtual base class ex 2
class A
{
public:
int i;
};

class B : virtual public A


{
public:
int j;
};

class C: virtual public A


{
public:
int k;
};

class D: public B, public C


{
public:
int sum;
};
void main()
{
D ob;
ob.i = 10; //unambiguous since only one copy of i is inherited.
ob.j = 20;
ob.k = 30;
ob.sum = ob.i + ob.j + ob.k;
cout << “Value of i is : ”<< ob.i;
cout << “Value of j is : ”<< ob.j;
cout << “Value of k is :”<< ob.k;
cout << “Sum is : ”<< ob.sum ;

return 0;
}