Monday, January 20, 2014

C++ Program to illustrate Virtual Function

with 0 Comment
#include<iostream.h>
#include<string.h>
#include<conio.h>
class media
{
protected:
char title[10];
float price;
public:
media(char *s,float p)
{
strcpy(title,s);
price=p;
}
virtual void display
{}
};
class book::public media
{
int pages;
public:
book(int p1,char *s,float p):media(s,p)
{
pages=p1;
}
void display()
{
cout<<"\t\t Virtual Function \n";
cout<<"\t\t --------------\n";
cout<<"\n Book Details \n";
cout<<"\n -----------\n";
cout<<"Title:"<<title<<"\n\n";
cout<<"Price:"<<price<<"\n\n";
cout<<"Pages:"<<pages<<"\n";
}
};
class tape:public media
{
float playtime
public:
tape(char *s,float p,float pt):media(s,p)
{
playtime=pt;
}
void display()
{
cout<<"\n Tape Details\n";
cout<<"\n ----------- \n";
cout<<"Title:"<<title<<"\n\n";
cout<<"Price:"<<price<<"\n\n";
cout<<"Playtime:"<<playtime<<"\n\n";
}
};

void main()
{
clrscr();
char *title=new char[30];
float price,playtime;
int pages;
cout<<"\t\t VIRTUAL FUNCTION\n";
cout<<"\t\t -------------------\n";
cout<<"\t\t Book Details\n";
cout<<"\n ------------\n";
cout<<"Title:";
cin>>title;
cout<<"\n";
cout<<"Price:";
cin>>price;
cout<<"\n";
cout<<"Pages:";
cin>>pages;
book b(pages,title,price);
cout<<"\n Tape Details\n";
cout<<"\n------------\n";
couot<<"Title:";
cin>>title;
cout<<"\n";
cout<<"Price:";
cin>>price;
cout<<"\n";
cout<<"Playtime:";
cin>>playtime;
tape t(title,price,playtime);
media *list[2];
list[0]=&b;
list[0]->display();
list[1]=&t;
list[1]->display();
getch();
}


-------------
OUTPUT:
-------------
VIRTUAL FUNCTION
-----------------------------
Book Details
----------------
Title : VB
Price:250.75
Pages:150

Tape Details
---------------
Title: NETWORK-CO
Price:75.25
Playtime:12

VIRTUAL FUNCTION
-----------------------------
Book Details
----------------
Title: JAVA
Price:250.75
Playtime:150

Tape Details
---------------
Title:NETWORK-CO
Price:75.25
Playtime:12

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive