slips 14

/* Slip no : 14_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
{
                int id;
                char name[20],author[20],pub[20];
                static int cnt;
                public:
                void getdata()
                {
                                cout<<"\nEnter book id : ";
                                cin>>id;
                                cout<<"\nEnter book name : ";
                                cin>>name;
                                cnt++;
                                cout<<"\nEnter author name : ";
                                cin>>author;
                                cout<<"\nEnter publication : ";
                                cin>>pub;
                }
                void display()
                {
                                //cout<<"\n\n*************OUTPUT*****************";
                                cout<<"\nbook id = "<<id;
                                cout<<"\nbook name = "<<name;
                                cout<<"\nAuthor name = "<<author;
                                cout<<"\npublication = "<<pub;
                }
                static void no_of_book()
                {
                                cout<<"\nNumber of book = "<<cnt;
                }
};

int book::cnt;

void main()
{
                clrscr();
                book b[20];int n;
                cout<<"\nEnter no f Books : ";
                cin>>n;
                for(int i=0;i<n;i++)
                                b[i].getdata();
                cout<<"\n Book Information are : \n \n";
                for( i=0;i<n;i++)
                                b[i].display();
                b[i-1].no_of_book();
                getch();

}

/* Slip no : 14_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>
class shape
{
public:
virtual void accept()=0;
virtual void area()=0;
};

class circle:public shape
{
public:
int r;
void accept()
{
  cout<<"\n Enter radius for circle : ";
  cin>>r;
}
void area()
{
  cout<<"\n Area of circle="<<3.14*r*r;
}
};

class rectangle:public shape
{
public:
int l,b;
void accept()
{
  cout<<"\n Enter length and breadth for rectangle : ";
  cin>>l;
  cin>>b;
}

void area()
{
   cout<<"\n Area of rectangle="<<l*b;

}
};

class triangle:public shape
{
  public:
  int b,h;
  void accept()
  {
     cout<<"\n Enter base and height for triangle : ";
     cin>>b;
     cin>>h;
  }

  void area()
  {
     cout<<"\n Area of triangle="<<0.5*b*h;
  }
};

void main()
{
     triangle t;
     circle c;
     rectangle r;
     shape *ptr;
     clrscr();
     ptr=&t;
     ptr->accept();
     ptr->area();
     ptr=&c;
     ptr->accept();
     ptr->area();
     ptr=&r;
     ptr->accept();
     ptr->area();
     getch();
}
slips 14 slips 14 Reviewed by Dinesh Varal on January 17, 2018 Rating: 5

No comments:

Powered by Blogger.