C++
--------
VIRTUAL FUNCTION
-----------------------------
When we use the same function name in both base and derived class the function in base class is declare as virtual using the keyword virtual. when a fun made virtual C++ determine which function to use at runtime base on the type of object pointed by the base pointer. by making the base pointer to point to the different version of virtual function.
RULES OF VIRTUAL FUNCTION
--------
VIRTUAL FUNCTION
-----------------------------
When we use the same function name in both base and derived class the function in base class is declare as virtual using the keyword virtual. when a fun made virtual C++ determine which function to use at runtime base on the type of object pointed by the base pointer. by making the base pointer to point to the different version of virtual function.
RULES OF VIRTUAL FUNCTION
- virtual funtion must be member of same class.
- they cannot be static members.
- they are access by using object variable.
- virtual function can be friend of another class.
- virtual function in a base class must be define even it is not use.
e.g -
#include<iostream.h>
#include<conio.h>
class A
{
public:
public:
virtual void display()
{
cout<<"bye";
cout<<"bye";
}
};
};
class B: public A
{
public:
public:
void display()
{
cout<<"hello";
cout<<"hello";
}
}:
}:
void main()
{
A *ptr;
A *ptr;
A a;
B b;
ptr=&a;
ptr->display();
ptr=&b;
ptr->display();
getch();
}
PURE VIRTUAL FUNCTION
-------------------------------------
In a class if any function is declare with zero and specify by keyword virtual that function is called pure virtual function.
Syntax:
virtual void display=0;
In a class if any function is pure virtual function that class become a abstract class and also that function define the class which is derived from it. we cannot create the object of abstract class.
e.g-
#include<iostream.h>
#include<conio.h>
class A
{
public:
public:
virtual void show=0;
void display();
{
cout<<"hello";
cout<<"hello";
}
};
};
class B: public A
{
public:
public:
void show()
{
cout<<"bye";
cout<<"bye";
};
void main()
{
B b;
B b;
b.show();
b.display();
getch();
}
Q) write a program in c++ to create a base class shape derived three different classes circle,rectangle and triangle from shape class. and calculate area of circle, rectangle and triangle use pure virtual function.
----->
#include<iostream.h>
#include<conio.h>
class Shape
{
public:
virtual void area()=0;
};
class circle:public shape
{
float r,a;
float r,a;
public:
circle()
{
cout<<"Enter radius:=";
cout<<"Enter radius:=";
}
void area()
void area()
{
a=3.14*r*r;
a=3.14*r*r;
cout<<endl<<"area of circle:="<<a;
}
};
};
class triangle:public shape
{
int a,b,h;
int a,b,h;
public:
triangle()
{
cout<<endl<<"enter base and height for triangle";
cin>>b>>n;
}
void area()
void area()
{
a=(b*h)/2;
a=(b*h)/2;
cout<<endl<<endl<<"area of triangle="<<a;
}
};
class rectangle:public shape
{
int a,l,b;
public:
rectangle()
{
cout<<endl<<"Enter length and breadth:";
cout<<endl<<"Enter length and breadth:";
cin<<l<<b;
void area()
{
a=l*b;
a=l*b;
cout<<endl<<"area of rectangle"<<a;
}
};
};
void main()
{
clrscr();
clrscr();
circle();
c1.area();
rectangle r1;
r1.area();
triangle t1;
t1.area();
getch();
}
--------------------------------------------------------------------------------
TEMPLATE
---------------
FUNCTION TEMPLATE
-------------------------------
Function template provide the capability of difining a single function that a scalaton or template for family orfunction with different argument types A function template is also called as generic function.
e.g -
Q)Write a program addition of two numbers two int and two floats values.(use function template).
--->
#include<iostream.h>
#include<conio.h>
template<class T>
void add(Ta,Tb)
{
c;
c;
c=a+b;
cout<<"addition is"<<c;
}
void main()
{
int x=10,y=6;
int x=10,y=6;
add(x,y);
float p=1.6,q=1.6;
add(p,q);
getch();
}
Q)Write a program to maximum of two number (using template) two float and two int value.
---->
#include<iostream.h>
#include<conio.h>
template<class T>
void max(Ta,Tb)
{
if(a>b)
{
cout<<"maximum is "<<a;
cout<<"maximum is "<<a;
}
if(b>a)
if(b>a)
{
cout<<"maximum is"<<b;
cout<<"maximum is"<<b;
}
void main()
{
int x=10,y=10;
max(x,y);
float p=1.6,q=1.6;
max(p,q)
getch();
}
CLASS TEMPLATE
-------------------------
A class template is a class that discribe the family related class. it create scalaton of class and structure of a class which contains one or more types that are generic or parametrized. A class template is also called a paramatrized class.
Syntax -
template<class T>
class class-name
{
//data elements of type T
//data elements of type T
//function with argument of type T
}
e.g -
#include<iostream.h>
#include<conio.h>
template<class T1,class T2>
class pair
{
T1 x;
T1 x;
T2 y;
public:
pair(T1 a,T2 b)
{
x=a;
x=a;
y=b;
}
void display()
void display()
{
cout<<x<<y;
cout<<x<<y;
}
};
};
void main()
{
pair<int,char>p1(25,'B')
pair<int,char>p1(25,'B')
p1.display();
pair<char,char>p2(Q,a)
p2.display();
getch();
}
Q) write a program in c++ to create a class swap and perform the operation swapping of two integers and two floats numbers(use class template)
--->
#include<iostream.h>
#include<conio.h>
template<class T1,class T2>
class swap
{
T1 a;
T1 a;
T2 b;
public:
swap(T1a,T2b)
{
x=a;
x=a;
y=b;
}
void display
void display
{
a=a+b;
a=a+b;
b=a-b;
a=a-b;
cout<<x<<y;
}
};
void main()
{
swap<int,int>p1(2,3)
swap<int,int>p1(2,3)
p1.display();
swap<float,float>p2(1.0,2.0)
p2.display();
getch();
}
---------------------------------------------------------------------------------------------
Q) A book (ISBN) and CD (data capacity) are both types of media(ID,title) objects. A person buys 10 media items, each of which can be either book or CD, display the list of all book and CD's bought. define the classes and appropriate member functions to accept and display data. use pointer concepts of polymorphism(virtual function)
---->
#include<iostream.h>
#include<string.h>
using namespace std;
class Media
{
protected:
protected:
int ID;
char title[20];
public:
media();
media(int,char*);
virtual void accept()
{
cout<<"Enter the media ID"<<endl;
cout<<"Enter the media ID"<<endl;
cin>>ID;
cout<<"Enter the title of the media"<<endl;
cin>>title;
}
virtual void diaplay()
virtual void diaplay()
{
cout<<"media ID\tTitle"<<endl;
cout<<"media ID\tTitle"<<endl;
}
};
Media::Media()
{
ID=0;
ID=0;
strcpy(this->title,title);
}
class Book:public Media
class Book:public Media
{
private:
private:
char ISBN[20];
public:
Book();
Book();
Book(char*);
void accept();
void display();
};
Book::Book()
{
strcpy(ISBN,"MD0001");
strcpy(ISBN,"MD0001");
}
Book::Book(char *ISBN_code)
Book::Book(char *ISBN_code)
{
strcpy(ISBN,ISBN_code);
}
void Book::accept()
void Book::accept()
{
cout<<"Enter the ISBN code for the Book:";
cout<<"Enter the ISBN code for the Book:";
cin>>ISBN;
}
void Book::display()
void Book::display()
{
cout<"ISBN code for the book:"<<ISBN<<endl;
cout<"ISBN code for the book:"<<ISBN<<endl;
}
class CD::public Media
class CD::public Media
{
private:
private:
float dataCapacity;
public:
CD();
CD(int);
void accept();
void dispay();
};
CD::CD()
{
dataCapacity=0.0;
dataCapacity=0.0;
}
CD::CD(int capacity)
{
dataCapacity=capacity;
dataCapacity=capacity;
}
void CD::accept()
void CD::accept()
{
cout<<"enter the data capacity of the compact disk in GB(Giga Bytes):";
cout<<"enter the data capacity of the compact disk in GB(Giga Bytes):";
cin>>dataCapacity;
}
void CD::display()
{
cout<<"the data capacity of the compact Disk:"<<dataCapacity<<"GB"<<endl;
cout<<"the data capacity of the compact Disk:"<<dataCapacity<<"GB"<<endl;
}
int main()
{
char type[20];
char type[20];
Media *md[10];
Book b[10];
CD c[10];
for(int i=0;i<10;i++)
{
again:cout<<"Enter type for Media Number"<<i+1<<"(BOOK/CD):";
cin>>type;
if(strcpy(type,"BOOK")==0)
{
b[i]=Book();
b[i]=Book();
b[i].accept();
md[i]=&b[i];
}
else
if(strcmp(type,"CD")==0)
{
c[i]=CD();
c[i].accept();
md[i]=&c[i];
}
else
{
cout<<"enter a correct choice(Either Book or CD)"<<endl;
goto again;
}
cout<<endl;
for(int i=0;i<10;i++)
{
md[i]->display();
md[i]->display();
cout<<endl;
}
return 0;
return 0;
}
OUTPUT -
Enter type for Media Number 1(BOOK/CD):BOOK
Enter the ISBN code for the Book: 147258369
Enter type for Media Number 2(BOOK/CD):BOOK
Enter the ISBN code for the Book: 798456123
Enter type for Media Number 3(BOOK/CD):CD
Enter the Data Capacity of the compact Disk in GB(Giga Bytes):4.77
Enter type for Media Number 4(BOOK/CD):CD
Enter the Data Capacity of the compact Disk in GB(Giga Bytes):9.5
Enter type for Media Number 5(BOOK/CD):BOOK
Enter the ISBN code for the Book: 159482637
Enter type for Media Number 6(BOOK/CD):CD
Enter the Data Capacity of the compact Disk in GB(Giga Bytes):8
Enter type for Media Number 7(BOOK/CD):CD
Enter the Data Capacity of the compact Disk in GB(Giga Bytes):10
Enter type for Media Number 8(BOOK/CD):BOOK
Enter the ISBN code for the Book: 784951623
Enter type for Media Number 9(BOOK/CD):CD
Enter the Data Capacity of the compact Disk in GB(Giga Bytes):11
Enter type for Media Number 7(BOOK/CD):BOOK
Enter the ISBN code for the Book: 987654321
ISBN code for the Book:147258369
ISBN code for the Book: 798456123
The Data Capacity of the compact Disk:4.77GB
The Data Capacity of the compact Disk:9.5GB
ISBN code for the Book: 159482637
The Data Capacity of the compact Disk:8GB
The Data Capacity of the compact Disk:10GB
ISBN code for the Book: 784951623
The Data Capacity of the compact Disk:11GB
Enter the ISBN code for the Book: 987654321
------------------------------------------------------------------------------------------------
0 Comments