OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals...

79
OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-1 AIM :-Write a Program in C++ to print “Hello World”. #include<iostream.h> #include<conio.h> class person { public: void display() { cout<<"hello world"; } }; void main() { person p; p.display(); getch(); } OutPut :- PRACTICAL-2 AIM :- Write a program in C++ Addition, Subtraction, Multiplication, Division by using object oriented class. #include<iostream.h> #include<conio.h> class a { public: void add(inta,int b) { int c=a+b; cout<<"addition is "; cout<<c<<endl; } void sub(inta,int b) { int c=a-b; cout<<"substraction is "; cout<<c<<endl; } voidmul(inta,int b)

Transcript of OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals...

Page 1: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

PRACTICAL-1

AIM :-Write a Program in C++ to print “Hello World”.

#include<iostream.h>

#include<conio.h>

class person

{

public:

void display()

{

cout<<"hello world";

}

};

void main()

{

person p;

p.display();

getch();

}

OutPut :-

PRACTICAL-2

AIM :- Write a program in C++ Addition, Subtraction, Multiplication, Division by using

object oriented class.

#include<iostream.h>

#include<conio.h>

class a

{

public:

void add(inta,int b)

{

int c=a+b;

cout<<"addition is ";

cout<<c<<endl;

}

void sub(inta,int b)

{

int c=a-b;

cout<<"substraction is ";

cout<<c<<endl;

}

voidmul(inta,int b)

Page 2: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

{

int c=a*b;

cout<<"multiplication is ";

cout<<c<<endl;

}

void div(inta,int b)

{

int c=a/b;

cout<<"division is ";

cout<<c<<endl;

}

};

void main()

{

a dis;

clrscr();

dis.display();

dis.add(30,20);

dis.sub(40,7);

dis.mul(42,2);

dis.div(12,2);

getch();

}

OutPut ;-

PRACTICAL-3

Aim :- Write a program in C++ to find the distance between two difference.

#include<iostream.h>

#include<conio.h>

//using namespace std;

class point

{

inta,b;

public: void point1()

{

cout<<"Enter value of point p:";

cin>>a>>b;

}

voidshowpoint()

{

Page 3: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

cout<<"point p="<<"("<<a<<","<<b<<")"<<endl;

}

void addition(point p)

{

cout<<"addition is p=("<<(a+p.a)<<","<<(b+p.b)<<")"<<endl;

}

};

int main()

{

point p1,p2;

clrscr();

p1.point1();

p1.showpoint();

p2.point1();

p2.showpoint();

p1.addition(p2);

getch();

}

OutPut :-

PRACTICAL-4

Aim :- Write a program making calculator using in C++.

#include<iostream.h>

#include<conio.h>

classcalc

{

floata,b;

public:

voidsetdata()

{

cout<<"Enter the value of a:";

cin>>a;

cout<<"Enter the value of b:";

cin>>b;

}

int addition()

{

Page 4: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

returna+b;

}

int subtraction()

{

return a-b;

}

int multiplication()

{

return a*b;

}

int division()

{

return a/b;

}

};

int main()

{

calc c;

intch;

int r;

clrscr();

c.setdata();

do

{

cout<<"Enter 1 for Addition"<<endl;

cout<<"Enter 2 for Subtraction"<<endl;

cout<<"Enter 3 for Multiplication"<<endl;

cout<<"Enter 4 for Division"<<endl;

cout<<"Enter 5 for Exit"<<endl;

cin>>ch;

switch(ch)

{

case 1:

cout<<"Addition="<<c.addition();

break;

case 2:

cout<<"Subtraction="<<c.subtraction();

break;

case 3:

cout<<"Multiplication="<<c.multiplication();

break;

case 4:

cout<<"Division="<<c.division();

break;

case 5:

exit();

Page 5: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

}

}while(ch!=5);

}

OutPut ;-

PRACTICAL-5

AIM:- Write a program in C++ to check number is prime or not.

#include<iostream.h>

#include<conio.h>

class prime

{

public:

void getnum()

{

int n;

cout<<"\nEnter Positive integer:";

cin>>n;

}

Page 6: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

void check()

{

int i,n,flag=0;

for(i=2;i<=n/2;i++)

{

if(n%i==0)

{

flag=1;

break;

}

}

if(flag==0)

{

cout<<"It is a Prime number";

}

else

{

cout<<"It is not a Prime number";

}

}

};

void main()

{

int j=1;

clrscr();

prime p;

do

{

p.getnum();

p.check();

j++;

} while(j<=5);

getch();

}

Output:-

Page 7: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

PRACTICAL-6

Aim :- Write a program in C++for multiplication two number and to find cube of numbers

using inline function.

#include<iostream.h>

#include<conio.h>

classabc

{

public:

intx,y;

voidgetnum()

{

cout<<"enter the value of x : ";

cin>>x;

cout<<"enter the value of y : ";

cin>>y;

cout<<"x = "<<x<<"\ty = "<<y<<endl;

}

inlineintcubex()

{

return(x*x*x);

}

inlineintcubey()

{

return(y*y*y);

}

inlineintmul()

{

return(x*y);

}

};

int main()

{

abc j;

clrscr();

j.getnum();

cout<<"cube of x : "<<j.cubex()<<endl;

cout<<"cube of y : "<<j.cubey()<<endl;

cout<<"Multiplication : "<<j.mul();

getch();

}

OutPut :-

Page 8: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

PRACTICAL-7

Aim : Write a program to find a mean of a given number using friend function.

#include<iostream.h>

#include<conio.h>

class base

{

int val1,val2;

public:

void get()

{

cout<<"Enter two values:";

cin>>val1>>val2;

}

friend float mean(base ob);

};

float mean(base ob)

{

return float(ob.val1+ob.val2)/2;

}

void main()

{

clrscr();

baseobj;

obj.get();

cout<<"\n Mean value is : "<<mean(obj);

getch();

}

OUTPUT:-

Page 9: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

PRACTICAL-8

AIM : WAP in C++ to calculate area and perimeter using virtual function.

# SOURCE CODE

#include<iostream.h>

#include<conio.h>

class Rectangle

{

public :

floatlength,breadth;

Rectangle(){}

Rectangle(float t,float t1)

{

length=t;

breadth=t1;

}

void get()

{

cout<<"\n\n\t\tEnter the Length of the Rectangle :";

cin>>length;

cout<<"\n\n\t\tEnter the Breadth of the Rectangle : ";

cin>>breadth;

}

virtual void calculate()

{

float p=2.0;

float k=length+breadth;

p=p*k;

cout<<"\n\n\tPERIMETER OF THE RECTANGLE : "<<p;

}

};

class Area : public Rectangle

{

public:

void calculate()

Page 10: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

{

cout<<"\n\n\tAREA OF THE RECTANGLE : "<<length*breadth;

}

};

void main()

{

clrscr();

cout<<"\n\n\tPROGRAM TO CALCULATE AREA & PERIMETER OF THE

RECTANGLE\n\n";

Area area;

Rectangle *ptr;

ptr=&area;

ptr->get();

ptr->calculate();

Rectangle rect(ptr->length,ptr->breadth);

ptr=&rect;

ptr->calculate();

getch();

}

OUTPUT :

PRACTICAL-9

AIM : WAP to calculate the area of circle, rectangle and triangle using function

overloading.

# SOURCE CODE

#include<iostream.h>

Page 11: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

#include<stdio.h>

#include<conio.h>

#define pi 3.14

classfn

{

public:

void area(int);

void area(int,int);

void area(float ,int,int);

};

voidfn::area(int a)

{

cout<<"Area of Circle:"<<pi*a*a;

}

voidfn::area(inta,int b)

{

cout<<"Area of rectangle:"<<a*b;

}

voidfn::area(float t,inta,int b)

{

cout<<"Area of triangle:"<<t*a*b;

}

void main()

{

intch;

inta,b,r;

clrscr();

fnobj;

cout<<"\n\t\tFunction Overloading";

cout<<"\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n:”;

cout<<”Enter your Choice:";

cin>>ch;

switch(ch)

{

case 1:

cout<<"Enter Radious of the Circle:";

cin>>r;

obj.area(r);

break;

case 2:

cout<<"Enter Sides of the Rectangle:";

cin>>a>>b;

obj.area(a,b);

Page 12: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

break;

case 3:

cout<<"Enter Sides of the Triangle:";

cin>>a>>b;

obj.area(0.5,a,b);

break;

case 4:

exit(0);

}

getch();

}

OUTPUT :

Page 13: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

PRACTICAL-10

AIM : Create a class named student having data member student name, roll no, and four

subject marks.

# SOURCE CODE

#include<iostream.h>

#include<conio.h>

class student

{

public:

int mark[4]; //data member

doublerollno; //data member

char name[20]; //data member

voidgetdata(); //function defined outside class

void display(); //function defined outside class

void exit();

};

void student::getdata()

{

cout<<"Enter the student name:-";

cin>>name;

cout<<"Enter the rollno.:-";

cin>>rollno;

for(int i=1;i<=4;i++)

{

cout<<"enter the marks of subject("<<i<<")=";

cin>>mark[i];

}

}

void student::display()

{

cout<<"\n\nStudent name:-"<<name<<endl;

Page 14: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

cout<<"Enrollment No.:-"<<rollno<<endl;

for(int j=1;j<=4;j++)

{

cout<<"Marks of subject("<<j<<")="<<mark[j]<<endl;

}

}

void main()

{

int a;

student s1[4];

clrscr();

cout<<"**********student detail******"<<endl;

cout<<"How many record will add=";

cin>>a;

for(int k=1;k<=a;k++)

{

s1[k].getdata();

}

for(int l=1;l<=a;l++)

{

s1[l].display();

}

getch();

}

OUTPUT :

Page 15: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

PRACTICAL-11

AIM : WAP to demonstrate concept of constructor.

# SOURCE CODE

#include<iostream.h>

#include<conio.h>

class s

{

public:

s()

{

}

s(intx,int y)

{

int z;

z=x+y;

cout<<"x : "<<x<<endl<<"y : "<<y<<endl;

cout<<"x + y = "<<z;

}

};

void main()

{

clrscr();

s(10,12);

getch();

}

OUTPUT :

PRACTICAL-12

AIM : WAP to calculate factorial of a given number using copy constructor.

# SOURCE CODE

#include<iostream.h>

#include<conio.h>

class copy{

inta,b;

public:

copy(int t)

{

Page 16: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

a=t;

}

double c()

{

b=1;

for(int i=1;i<=a;i++)

{

b=b*i;

}

return b;

}

};

void main()

{

int n;

clrscr();

cout<<"\n\t Enter the no :";

cin>>n;

copyobj(n);

copy copy=obj;

cout<<"\n\t"<<"factorial is :"<<obj.c();

getch();

}

OUTPUT :

PRACTICAL-13

AIM : WAP in C++ to concat two string using operator overloading.

# SOURCE CODE

#include<conio.h>

#include<string.h>

#include<iostream.h>

class string

{

charstr[100];

public:

void input();

void output();

string operator+(string s);

Page 17: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

};

void string::input()

{

cout<<"enter the string\n";

cin>>str;

}

string string::operator+(string s)

{

string temp;

strcpy(temp.str,str);

strcat(temp.str,s.str);

return(temp);

}

void string::output()

{

cout<<"the string is\n";

cout<<str;

}

void main()

{

string s1,s2,s3;

clrscr();

s1.input();

s2.input();

s3=s1+s2;

s3.output();

getch();

}

OUTPUT :

PRACTICAL-14

AIM : WAP to perform Binary operator overloading using C++ programming.

# SOURCE CODE

#include<iostream.h>

#include<conio.h>

class complex

Page 18: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

{

inta,b;

public:

voidgetvalue()

{

cout<<"Enter the value of Complex Numbers a,b:";

cin>>a>>b;

}

complex operator+(complex ob)

{

complex t;

t.a=a+ob.a;

t.b=b+ob.b;

return(t);

}

complex operator-(complex ob)

{

complex t;

t.a=a-ob.a;

t.b=b-ob.b;

return(t);

}

void display()

{

cout<<a<<"+"<<b<<"i"<<"\n";

}

};

void main()

{

clrscr();

complex obj1,obj2,result,result1;

obj1.getvalue();

obj2.getvalue();

result = obj1+obj2;

result1=obj1-obj2;

cout<<"Input Values:\n";

obj1.display();

obj2.display();

cout<<"Result:";

result.display();

result1.display();

getch();

}

Page 19: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

OUTPUT :

PRACTICAL-15

AIM : WAP to find out the payroll system using single inheritance.

# SOURCE CODE

#include<iostream.h>

#include<conio.h>

classemp{

public:

inteno;

char name[20],des[20];

void get()

{

cout<<"Enter the Employee no:";

cin>>eno;

cout<<"Enter the Employee name:";

cin>>name;

cout<<"Enter the Designation:";

cin>>des;

}

};

classsalary:publicemp

{

floatbp,hra,da,pf,np;

public:

void get1(){

cout<<"Enter the basic pay:";

cin>>bp;

cout<<"Enter the Human resource allowance:";

Page 20: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

cin>>hra;

cout<<"Enter the Dearness allowance:";

cin>>da;

cout<<"Enter the Profitablity fund:";

cin>>pf;

}

void calculate()

{

np=bp+hra+da-pf;

}

void display()

{

cout<<eno<<"\t "<<name<<"\t "<<des<<"\t "<<bp<<"\t "<<hra<<"\t "<<da<<"\t "<<pf<<"\t

"<<np<<"\n ";

}

};

void main()

{

inti,n,no;

charch;

salary s[10];

clrscr();

cout<<"Enter no. of Employee:";

cin>>no;

for(i=0;i<no;i++)

{

s[i].get();

s[i].get1();

s[i].calculate();

}

cout<<"\n e_no \t e_name \t des \t bp \t hra \t da \t pf \t np \n";

for(i=0;i<no;i++)

{

s[i].display();

}

getch();

}

OUTPUT :

Page 21: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

PRACTICAL-16

AIM : WAP to find out the student details using multiple inheritances.

# SOURCE CODE

#include<iostream.h>

#include<conio.h>

class s{

public:

longint e;

char n[10];

void get()

{

cout<<"Enter the name of student : ";

cin>>n;

cout<<"\nEnter the er.no : ";

cin>>e;

}

};

class s1{

public:

double year;

char branch[10];

void get1()

{

cout<<"\nEnter the current year : ";

cin>>year;

cout<<"\nEnter the branch : ";

cin>>branch;

}

};

classstudent:public s1,public s

{

Page 22: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

public:

float r;

void get2()

{

cout<<"\nEnter the result : ";

cin>>r;

}

void display()

{

cout<<"\nName : "<<n<<"\nE_no : "<<e<<"\nBranch :

"<<branch<<"\nYear"<<year<<"\nResult :"<<r;

}

};

void main()

{

student s2;

clrscr();

s2.get();

s2.get1();

s2.get2();

s2.display();

getch();

}

OUTPUT :

Page 23: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

PRACTICAL-17

AIM : Derive two classes son and daughter and demonstrate polymorphism in action.

# SOURCE CODE

#include<iostream.h>

#include<conio.h>

//using namespace std;

class father

{

protected:

int age;

public:

father(int a)

{

age=a;

}

virtual void iam()

{

cout<<"I am father. My age is:"<<age;

}

};

classson:public father

{

private:

int ages;

public:

son(inty,int x):father(x)

{

ages=y;

}

voidiam()

{

cout<<endl<<"I am son.My age is: "<<ages;

}

};

class daughter: public father

{

private:

int aged;

public:

daughter(intc,int l):father(l)

{

aged=c;

}

voidiam()

{

Page 24: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

cout<<endl<<"I am daughter.My age is: "<<aged;

}

};

void main()

{

clrscr();

father f(50);

son s(22,24);

daughter d(18,25);

f.iam();

s.iam();

d.iam();

father *bptr;

bptr=&s;

bptr->iam();

bptr=&d;

bptr->iam();

getch();

}

OUTPUT :

PRACTICAL-18

AIM : WAP to create a class to represent bank account. Include the following Data

Members:

Name of the Depositor

Account Number

Type of Account

Balance amount in Account

Member Function

To assign initial values

To deposit an amount

To withdraw an amount after checking balance

To display name and Balance

# SOURCE CODE

#include<iostream.h>

#include<conio.h>

Page 25: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

class bank

{

public:

char name[50];

doubleacc_no;

charac_typ[10];

double balance;

voidgetdata()

{

balance = 10000;

cout<<"Inital balance : "<< balance <<endl;

cout<<"Enter the name of depositor : "<<endl;

cin>>name;

cout<<"Enter The acc_no : "<<endl;

cin>>acc_no;

cout<<"Enter the acc_type : "<<endl;

cin>>ac_typ;

}

void deposit()

{

int d;

cout<<"Enter amount to be deposit : "<<endl;

cin>>d;

balance=balance+d;

}

void withdraw()

{

int w;

cout<<"Enter the withdraw amount : "<<endl;

cin>>w;

balance=balance-w;

}

void display()

{

cout<<"Name of Account holder : "<<name<<endl<<"Balance : "<<balance<<endl;

}

};

void main()

{

bank b;

clrscr();

b.getdata();

b.deposit();

b.withdraw();

b.display();

getch();

Page 26: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

}

OUTPUT :

PRACTICAL-19

AIM : Create two classes DM and DB.DM stores distance in meters and centimeters. DB

stores in a feet and inches. WAP that can read values for a class object and add one object

of DM with another and add one object of DM with another object DB.Use of friend

function to carry out the addition operation. The object that stores result may be a DB

object or may be a DM object, depending on the units in which result is required.

# SOURCE CODE

#include<iostream.h>

#include<conio.h>

class DB;

class DM

{

intmetres;

floatcentimetres;

public:

DM()

{

metres=0;centimetres=0.00;

Page 27: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

}

DM(intm,float cm)

{

metres=m;

centimetres=cm;

}

intget_m()

{

returnmetres;

}

floatget_cm()

{

returncentimetres;

}

voidgetdata()

{

cout<<"enter metres:";

cin>>metres;

cout<<"enter centimetres:";

cin>>centimetres;

}

void display()

{

cout<<metres<<"m-"<<centimetres<<"cm";

}

friend DM add(DM,DB);

};

class DB

{

int feet;

float inches;

public:

DB()

{

feet=0;

inches=0.00;

}

DB(intf,float i)

{

feet=f;

inches=i;

}

DB(DM dm)

{

int m;

floatcm,t_cm,t_in;

Page 28: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

m=dm.get_m();

cm=dm.get_cm();

t_cm=m*100+cm;

t_in=t_cm/2.54;

feet=int(t_in)/12;

inches=t_in-feet*12;

}

operator DM()

{

float in=feet*12+inches;

float cm=in*2.54;

intmtr=int(cm)/100;

floatcmtr=cm-mtr*100;

return DM(mtr,cmtr);

}

voidgetdata()

{

cout<<"enter feet:";

cin>>feet;

cout<<"enter inches:";

cin>>inches;

}

void display()

{

cout<<feet<<"\'-"<<inches<<"\"";

}

friend DM add(DM,DB);

};

DM add(DM dm,DBdb)

{

DM a=db;

int m=dm.metres+a.metres;

float cm=dm.centimetres+a.centimetres;

if(int(cm)>=100)

{

cm-=100.00;

m++;

}

return DM(m,cm);

}

int main()

{

DB db,db1;

DM dm,dm1;

clrscr();

cout<<"enter distance d1(in metres&centimetres):\n";

Page 29: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

dm.getdata();

cout<<"enter distance d2(in feet & inches):\n";

db.getdata();

dm1=add(dm,db);

db1=add(dm,db);

dm.display();cout<<" + ";db.display();cout<<" = ";dm1.display();

cout<<" = ";

db1.display();

getch();

return 0;

}

OUTPUT :

PRACTICAL-20

AIM : WAP to add new book in book shop database.

# SOURCE CODE

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<string.h>

class stock

{

char author[50];

char title[50];

char pub[50];

double price;

intnumcopies;

public:

stock();

intaccess_title(char a[]);

Page 30: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

void input();

voidgetdata(int);

};

stock::stock()

{

char author[50]={"abc"};

char title[50]={"efg"};

char pub[50]={"hij"};

price=500;

numcopies=50;

}

int stock::access_title(char a[])

{

if(strcmp(title,a))

return 0;

else return 1;

}

void stock::getdata(intnum)

{

if(numcopies>=num)

cout<<"\nCost of "<<num<<" books is Rs. "<<(price*num);

else

cout<<"\nSorry! These many copies are unavailable!";

}

void stock::input()

{

cout<<"\nTitle: ";

gets(title);

cout<<"\nAuthor:";

gets(author);

cout<<"\nPublisher:";

gets(pub);

cout<<"\nPrices:";

cin>>price;

cout<<"\ncopies available:";

cin>>numcopies;

}

void main()

{

clrscr();

stockobj[2];

int n;

charttle[50];

Page 31: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

cout<<"Enter details of 3 books";

for(int i=0;i<2;++i)

obj[i].input();

cout<<endl;

cout<<"\n Enter title of required book\n";

gets(ttle);

for(i=0;i<2;i++)

{

if(obj[i].access_title(ttle))

{

cout<<"\nHow many copies? ";

cin>>n;

obj[i].getdata(n);

}

else

cout<<"\nBook unavailable";

}

getch()

}

OUTPUT :

Page 32: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

PRACTICAL-21

AIM : WAP in C++ to convert lowercase to uppercase from a file.

# SOURCE CODE

#include<iostream.h>

#include<conio.h>

#include<string.h>

void main()

{

clrscr();

charstr[20];

int i;

cout<<"Enter the String (Enter First Name) in uppercase : ";

cin>>str;

for(i=0;i<=strlen(str);i++)

{

if(str[i]>=65 &&str[i]<=92)

{

str[i]=str[i]+32;

}

}

cout<<"\nThe String in Lowercase = "<<str;

getch();

}

OUTPUT :

PRACTICAL-22

AIM : WAP to swap the numbers using the concept of function.

# SOURCE CODE

#include<iostream.h>

#include<conio.h>

template<class T>

void swap(T&a,T&b)

{

T temp=a;

a=b;

b=temp;

}

Page 33: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

OOPC Practicals

GTU/BE/CE/VIEAT/OOPC++/SEM-4

int main()

{

int x1=16,y1=26;

float x2=16,y2=55.5;

clrscr();

cout<<"Before swap : "<<endl;

cout<<"x1 = "<<x1<<endl<<"y1 = "<<y1<<endl;

cout<<"x2 = "<<x2<<endl<<"y2 = "<<y2<<endl;

swap(x1,y1);

swap(x2,y2);

cout<<"After swap : "<<endl;

cout<<"x1 = "<<x1<<endl<<"y1 = "<<y1<<endl;

cout<<"x2 = "<<x2<<endl<<"y2 = "<<y2;

getch();

}

OUTPUT :

Page 34: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Vidhyadeep Institute of

engineering & Technology

Topic Name:-inheritance

Subject Name:-OOPC++

Subject code:-(2140701)

Page 35: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

INHERITANCE

“the mechanism by which one class acquires

the properties of another class”

Page 36: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

C++ AND INHERITANCE

The language mechanism by which one class

acquires the properties (data and operations)

of another class

Base Class (or superclass): the class being

inherited from

Derived Class (or subclass): the class that

inherits

Page 37: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

The mechanism of deriving a new class from an

old class is called inheritance.

The derived class may have all the features of

the base class and the programmer can add

new features to the derived class.

For example, Student is a base class and result

is derived class.

Page 38: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

ADVANTAGES OF INHERITANCE

When a class inherits from another class, there are three benefits:

You can reuse the methods and data of the existing class

You can extend the existing class by adding new data and new methods

You can modify the existing class by overloading its methods with your own implementations

Page 39: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

TYPES OF INHERITANCE

There are five types of inheritance:-

Single Inheritance

Multilevel Inheritance

Multiple Inheritance

Hierarchical Inheritance

Hybrid Inheritance

Page 40: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

SINGLE INHERITANCE

If a class is derived from a single class then

it is called single inheritance.

Class B is derived from class A.

Page 41: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

MULTILEVEL INHERITANCE

A class is derived from a class which is derived

from another class then it is called multilevel

inheritance.

E.g, class C is derived from class B and class B

is derived from class A, so it is called multilevel

inheritance.

Page 42: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

MULTIPLE INHERITANCE

If a class is derived from more then one class

then it is called multiple inheritance.

E.g, class C is derived from two classes, class

A and class B.

Page 43: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

HIERARCHICAL INHERITANCE

If one or more classes are derived from one

class then it is called hierarchical inheritance.

E.g, class B, class C and class D are derived

from class A.

Page 44: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

HYBRID INHERITANCE

It is a combination of any above inheritance

types.

That is either multiple or multilevel or

hierarchical or any other combination.

Page 45: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

THANK YOU

Page 46: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Vidhyadeep Institute Of Engineering And Technology

Computer Engineering Department

(4th Semester)

Subject:- Object Oriented Programming with

C++ (2140705)

Topic:- Operator Overloading &

Function Overloading

Page 47: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Operator overloading Enabling C++’s operators to work with class

objects Using traditional operators with user-

defined objects Requires great care; when overloading is

misused, program difficult to understand Examples of already overloaded operators

Operator << is both the stream-insertion operator and the bitwise left-shift operator

+ and -, perform arithmetic on multiple types Compiler generates the appropriate code

based on the manner in which the operator is used

Introduction

Page 48: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Overloading an operator Write function definition as normal Function name is keyword operator followed

by the symbol for the operator being overloaded operator+ used to overload the addition

operator (+)

Using operators To use an operator on a class object it must be

overloaded unless the assignment operator(=)or the address operator(&) Assignment operator by default performs member

wise assignment Address operator (&) by default returns the address of

an object

Introduction

Page 49: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Restrictions on Operator Overloading

C++ operators that can be overloaded

C++ Operators that cannot be overloaded

Operators that can be overloaded

+ - * / % ^ & |

~ ! = < > += -= *=

/= %= ^= &= |= << >> >>=

<<= == != <= >= && || ++

-- ->* , -> [] () new delete

new[] delete[]

Operators that cannot be overloaded

. .* :: ?: sizeof

Page 50: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Restrictions on Operator Overloading

• Overloading restrictions- Precedence of an operator cannot be changed- Associativity of an operator cannot be changed- Arity (number of operands) cannot be changed

• Unary operators remain unary, and binary operators remain binary• Operators &, *, + and - each have unary and binary versions• Unary and binary versions can be overloaded separately

• No new operators can be created- Use only existing operators

• No overloading operators for built-in types- Cannot change how two integers are added- Produces a syntax error

Page 51: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Overloading unary operators Can be overloaded with no arguments or one

argument

Should usually be implemented as member functions Avoid friend functions and classes because they

violate the encapsulation of a class

Example declaration as a member function:class String {

public:

bool operator!() const;

...

};

Overloading Unary Operators

Page 52: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Overloaded Binary operators Non-static member function, one argument

Example:class String {

public:

const String &operator+=(

const String & );

...

};

y += z is equivalent to y.operator+=( z )

Overloading Binary Operators

Page 53: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

A function name having several definitions that aredifferentiable by the number or types of theirarguments.

OR

Function Overloading not only implementspolymorphism but also reduces number ofcomparisons in a program and thereby makes theprogram run faster.

For example;

float divide (int a, int b);float divide (float x, float y);

Function Overloading

Page 54: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

When a function name is declared more than once in a program, thecompiler will interpret the second (and subsequent) declaration(s)as follows:

1) If the signatures of subsequent functions match the previousfunction’s, then the second is treated as a re-declaration of the first.

2) If the signatures of two functions match exactly but the return typediffer, the second declaration is treated as an erroneous re-declaration of the first and is flagged at compile time as an error.

For example,float square (float f);

double square (float x);

Functions with the same signature and same name but different returntypes are not allowed in C++. You can have different return types, butonly if the signatures are also different:

float square (float f);double square (double d);

Page 55: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

3) If the signature of the two functions differ ineither the number or type of their arguments,the two functions are considered to beoverloaded.

Use function overloading only when a function is required to work for alternative argument types and there is a definite way of optimizing the function for the argument type.

Page 56: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Several restrictions governs an acceptable set ofoverloaded functions:

Any two functions in a set of overloadedfunctions must have different argument lists.

Overloading functions with argument lists ofthe same types, based on return type alone, isan error.

Member functions cannot be overloadedsolely on the basis of one being static and theother nonstatic.

Restrictions on Overloaded Functions

Page 57: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Typedef declaration do not define new types; they introducessynonyms for existing types. They do not affect the overloadingmechanism. Consider the following code:

typedef char* PSTR;

void Print (char * szToPrint);

void Print (PSTR szToPrint);

Page 58: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Overloaded functions are called just like otherfunctions. The number and type ofarguments determine which function shouldbe invoked.

For instance consider the following codefragment:

prnsqr (‘z’);

prnsqr (13);

prnsqr (134.520000012);

prnsqr (12.5F);

CALLING OVERLOADED FUNCTIONS

Page 59: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Thank you…

Page 60: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

VIDYADEEP INSTITUTE OF

ENGINEERING AND TECHNOLOGY

COMPUTER DEPARTMENT

Subject:OOPC(2140705)

Page 61: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

INHERITANCE

“the mechanism by which one class acquires

the properties of another class”

Page 62: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

C++ and inheritance

• The language mechanism by which one

class acquires the properties (data and

operations) of another class

• Base Class (or superclass): the class being

inherited from

• Derived Class (or subclass): the class that

inherits

Page 63: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Advantages of inheritance

• When a class inherits from another class, there are three benefits:

• (1) You can reuse the methods and data of the existing class

(2) You can extend the existing class by adding new data and new methods

(3) You can modify the existing class by overloading its methods with your own implementations

Page 64: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Deriving One Class from Another (cont’d)

• Define a new class CountedQue from QueTypesuch that it has a new data member (length) that records the number of items in the queue

template<class ItemType>class CountedQue : public QueType<ItemType> {

public:CountedQue();void Enqueue (ItemType newItem); void Dequeue (ItemType& item); int LengthIs() const;

private:int length;

};

Page 65: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Inheritance and accessibility

• A class inherits the behavior of another

class and enhances it in some way

• Inheritance does not mean inheriting access

to another class’ private members

Page 66: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Rules for building a class

hierarchy• Derived classes are special cases of base classes

• A derived class can also serve as a base class for new classes.

• There is no limit on the depth of inheritanceallowed in C++ (as far as it is within the limits of your compiler)

• It is possible for a class to be a base class for more than one derived class

Page 67: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Polymorphism

• Any code you write to manipulate a base class will

also work with any class derived from the base class.

• C++ general rule for passing objects to a function:

“the actual parameters and their corresponding

formal parameters must be of the same type”

• With inheritance, C++ relaxes this rule:

“the type of the actual parameter can be a class

derived from the class of the formal parameter”

Page 68: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

An example

template<class ItemType>void Test(QueType& q, ItemType item){q.Enqueue(item);....

}

• Any object of a class derived from QueType can

be passed to the function !!

• Which Enqueue() function should be used? (the

compiler does not know that at compile time)

Page 69: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Static vs. dynamic binding

• Static Binding: the determination of which method

to call at compile time

• Dynamic Binding: the determination of which

method to call at run time

Page 70: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Virtual Functions

• C++ uses virtual functions to implement run-

time binding.

• To force the compiler to generate code that

guarantees dynamic binding, the word virtual

should appear before the function declaration

in the definition of the base class.

Page 71: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

• Rules for static/dynamic binding:

– If the member function of the base class is not

a virtual function, the type of the formal

parameter determines which function to call.

– If the member function of the base class is a

virtual function, the type of the actual

parameter determines which function to call.

Virtual Functions (cont.)

Page 72: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

An exampleclass ItemType {public:...virtual bool operator<(ItemType) const;

private: protected:StrType lastName;

};

bool ItemType::operator<(ItemType item) const{

int result;

result = strcmp(lastName, item.lastName);if(result < 0)return true;

elsereturn false;

}

Page 73: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Protected class members

• Derived classes cannot access the private

data of the base class

• Declaring methods and data of the base

class as protected (instead of private) allows

derived classes to access them

• Objects outside the class, however, cannot

access them (same as private)

Page 74: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Protected and Private Inheritanceclass X : protected Y {

...

};

• With protected inheritance, public and protected members of Y become protected in X (i.e., classes derived from X inherit the public members of Y as protected)

• With private inheritance, public and protected members of Y become private in X (i.e., classes derived from X inherit the public members of Y as private)

• Default inheritance: private

Y

X

Page 75: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Constructors and destructors

• You cannot override a base class constructor with

a derived class constructor (rather, the derived

class constructor calls the base class constructor

first)

• All base class destructors should be declared

virtual

• Virtual destructors are called in reverse order from

the constructors for derived class objects

Page 76: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Multiple Inheritance

• Derived classes can inherit from more than

one base classesX

Y

Z

(base for Y)

(base for Z)

Page 77: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

Define a new class LookAheadStack that is derived from class StackType.

(1) A look-ahead stack differs from the standard stack only in the push operation.

(2) An item is added to the stack by the push method only if its different from the top stack element.

Example

Page 78: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

template<class ItemType>struct NodeType;

template<class ItemType>class StackType {

public:StackType();~StackType();void MakeEmpty();bool IsEmpty() const;bool IsFull() const;void Push (ItemType);void Pop(ItemType&);

private:NodeType<ItemType>* topPtr;

};

Page 79: OOPC Practicalsdegree.vidhyadeep.org/study/Computer/4_OOPC/OOPC_PPT.pdf · OOPC Practicals GTU/BE/CE/VIEAT/OOPC++/SEM-4 PRACTICAL-7 Aim : Write a program to find a mean of a given

THANK YOU