Code Competitions

Coding competition

Saturday, 15 June 2013

program to use derived class pointer to point to base class by using explicit type casting

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class book
{
    private:
    char title[15];
    float price;
    int pon;
    public:
    void getdata()
    {
        cout<<"\nenter the title of book ";
        cin>>title;
        cout<<"\nprice of book";
        cin>>price;
        cout<<" no. of pages ";
        cin>>pon;
    }
    void display()
    {
        cout<<"\ntitle "<<title;
        cout<<" total pages "<<pon;
        cout<<" price "<<price;

    }
};
class sdetail:public book
{
    private:
    char agency[20];
    int id;
    public:
    void getdata()
    {
        cout<<" agency name ";
        gets(agency);
        cout<<" registration number ";
        cin>>id;

    }
    void display()
    {
        //book::display();
        cout<<" agency name "<<agency;
        cout<<" reg. no. "<<id;

    }
};
void main()
{
    clrscr();
    class sdetail *p,*q,y;
    class book x;
    p=q;
    p=(sdetail *) &x;
    p->getdata();
    p->display();
    getch();
}