打卡第二天

7-7

#include <iostream>

#include<string>

using namespace std;

class Document{  

private:

    string name;

public:

    Document(string nam):name(nam) 

    {

        cout<<"Name:"<<name<<endl;

    }

    ~Document(){}  

};

class Book:public Document{ 

private:

    int pageCount;

public:

    Book(string nam,int ag):Document(nam),pageCount(ag) 

    {

 

        cout<<"Page:"<<pageCount<<endl;

    }

    ~Book(){}

};

int main()

{

    int page;

    string name;

    cout<<"Input Name and Page:";

    cin>>name>>page;   

    Book b(name,page); 

    return 0;

}

7-9

#include  <iostream>

#include  <string>

using  namespace  std;

class  Object

{

public:

        Object()

        {

                cout<<"Constructing  Object!"<<endl;

        }

        ~Object()

        {

                cout<<"Destructing  Object!"<<endl;

        }

        void  getInfo()

        {

                cout<<"Input  weight:";

                cin>>weight;

        }

        void  showInfo()//类Object的成员函数

        {

                cout<<"Weight:"<<weight<<endl;

        }

private:

        int  weight;

};

class Box:public Object{

private:

    int height;

    int width;

public:

    Box()

    {

        cout<<"Constructing Box!"<<endl;

    }

    ~Box()

    {

        cout<<"Destructing Box!"<<endl;

    }

 

    void  getInfo()

    {

        Object::getInfo();

        cout<<"Input height and width:";

        cin>>height>>width;

    }

 

    void  showInfo()

        {

            Object::showInfo();

                cout<<"Height and width:"<<height<<","<<width<<endl;

        }

};

 

int  main()

{

        Box  box;

        box.getInfo();

        box.showInfo();

        return  0;

}

posted @ 2023-04-13 20:36  欧吼吼  阅读(14)  评论(0编辑  收藏  举报