Q.1) Write a C++ program to create a
class Book which contains data members as B_Id, B_Name, B_Author,
B_Publication. Write member functions to accept and display Book information
also display Count of books. (Use Static data member to maintain Count of
books)
#include<iostream.h>
#include<conio.h>
class book
{
static int count;
int bid;
char
bname[10],bauthor[10],bpublication[10];
public:
void get()
{
cout<<"\nEnter
id:\t";
cin>>bid;
cout<<"\nEnter
book name:\t";
cin>>bname;
cout<<"\nEnter
author:\t";
cin>>bauthor;
cout<<"\nEnter
publication:\t";
cin>>bpublication;
}
void display()
{
cout<<"\n\n1.Id\t"<<bid;
cout<<"\n2.Name\t"<<bname;
cout<<"\n3.Author\t"<<bauthor;
cout<<"\n3.Publication"<<bpublication;
}
void cnt(void)
{
count++;
cout<<"\n\nTotal
books:\t"<<count;
}
};
int book::count;
void main()
{
int i,n;
book b[10];
clrscr();
cout<<"How many
books you want to enter";
cin>>n;
for(i=0;i<n;i++)
{
b[i].get();
}
for(i=0;i<n;i++)
{
b[i].display();
}
for(i=0;i<n;i++)
{
b[i].cnt();
}
getch();
}
Q.2) Create a base class Shape.
Derive three different classes Circle, Rectangle and Triangle from Shape class.
Write a C++ program to calculate area of Circle, Rectangle and Triangle. (Use
pure virtual function).
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class shape
{
public:
virtual void area()=0;
};
class circle:public shape
{
public:
int r;
void getc()
{
cout<<"\nenter the radius:";
cin>>r;
}
void area();
};
class rectangle:public shape
{
public:
int l,b;
void getr()
{
cout<<"\nenter the length & breadth:";
cin>>l>>b;
}
void area();
};
class triangle:public shape
{
public:
int b1,h;
void gett()
{
cout<<"\nenter the base & height:";
cin>>b1>>h;
}
void area();
};
void circle::area()
{
cout<<(3.14*r*r);
}
void rectangle::area()
{
cout<<(l*b);
}
void triangle::area()
{
cout<<(0.5*b1*h);
}
void main()
{
clrscr();
int ch;
circle c;
rectangle rect;
triangle t;
do
{
cout<<"\n1.Area of circle";
cout<<"\n2.Area of rectangle";
cout<<"\n3.Area of triangle";
cout<<"\n4.Exit";
cout<<"\nEnter your choice:\t";
cin>>ch;
switch(ch)
{
case 1:
c.getc();
cout<<"Area of circle:";
c.area();
break;
case 2:
rect.getr();
cout<<"Area of rectangle:";
rect.area();
break;
case 3:
t.gett();
cout<<"Area of triangle:";
t.area();
break;
case 4:
exit(0);
}
}while(ch!=4);
getch();
};