zoukankan      html  css  js  c++  java
  • 学堂在线TsinghuaX: 00740043_2X C++语言程序设计进阶 第七章Lab

    第一题:账户类

    题目描述

    定义一个基类Account,数据成员包含string类变量userName用于保存账户主人姓名,函数成员包括默认构造函数、带参构造函数用于初始化数据成员和输出姓名的成员函PrintName()。从Account类派生出CreditAccount类,增加整型数据成员credit用于记录该用户信用额度,函数成员包括带参构造函数用于初始化数据成员和输出账户信息的成员函数PrintInfo()。要求:在函数PrintInfo()中需要调用基类的成员函数PrintName()。填充以下代码:

    #include <iostream>
        #include <string>
        using namespace std;
        
        class Account
        { 
             string userName;
        public:
             Account(){};
             Account( string name );
             void  PrintUserName();
        };
        
        class CreditAccount : public Account
        {
        public:
             CreditAccount( string name, int credit);
             void PrintInfo();
        private:
             int credit;
        };
        
        //请实现Account构造函数Account(char *name)
        //请实现Account的PrintUserName()函数
        //请实现CreditAccount类的构造函数CreditAccount(char* name, long number)
        //请实现CreditAccount类的PrintInfo()函数
        
        int main()
        {
             CreditAccount a("I Love CPP", 10000);
             a.PrintInfo();
             return 0;
        }

    输入描述

    输出描述

    输出共两行,第一行为账户姓名,第二行为账户信用额度

    样例输入

    样例输出

    I Love CPP
    10000
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Account
    {
        string userName;
    public:
        Account(string name) : userName(name){
            userName=name;
        }
        //Account(string name);
        void  PrintUserName(){
            cout << userName << endl;
        }
    };
    
    class CreditAccount : public Account
    {
    public:
        CreditAccount(string name, long number) : Account(name), credit(number){}
        void PrintInfo(){
            PrintUserName();
            cout << credit << endl;
        }
    private:
        long credit;
    };
    
    int main()
    {
        CreditAccount a("I Love CPP", 10000);
        a.PrintInfo();
        return 0;
    }

    第二题:多继承

    题目描述 

    下面的代码声明了三个基类Base1、Base2和Base3,然后从这三个基类按照公有方式派生出类Derived。在每个类中分别定义带一个整型参数的构造函数和析构函数输出提示信息,构造函数的提示信息中需要包含整型参数的数值。请将下面的代码补充完整,使得输出结果与样例输出相同,注意:测试数据有多组。 

    #include <iostream>
        using namespace std;
       
        class Base1
        {
        public:
            Base1(int x);
            ~Base1();
        };
       
        class Base2
        {
        public:
            Base2(int x);
            ~Base2();
        };
        class Base3
        {
        public:
            Base3(int x);
            ~Base3();
        };
       
        class Derived://继承上面3个类
        {
        public:
            Derived(int x1, int x2, int x3, int x4);
            ~Derived();
        };
       
        Base1::Base1(int x)
        {
            cout<<"Base1 constructor called "<<x<<endl;
        }
       
        Base1::~Base1()
        {
            cout<<"Base1 destructor called"<<endl;
        }
        //依照Base1类中的代码实现其它类的构造函数和析构函数 
       
       
        int main()
        {
            int x[4];
            for (int i = 0; i < 4; ++i)
                cin >> x[i];
            Derived d(x[0], x[1], x[2], x[3]);
            return 0;
        }

    输入描述

    每组输入为4个整数用空格隔开

    输出描述

    根据构造和析构函数的调用顺序输出

    样例输入

    1 2 3 4

    样例输出

    Base2 constructor called 3
    Base1 constructor called 2
    Base3 constructor called 4
    Derived constructor called 1
    Derived destructor called
    Base3 destructor called
    Base1 destructor called
    Base2 destructor called
    #include <iostream>
    using namespace std;
    
    class Base1
    {
    public:
        Base1(int x);
        ~Base1();
    };
    
    class Base2
    {
    public:
        Base2(int x);
        ~Base2();
    };
    class Base3
    {
    public:
        Base3(int x);
        ~Base3();
    };
    
    class Derived:public Base2,public Base1,public Base3
    {
    public:
        Derived(int x1, int x2, int x3, int x4):Base1(x2),Base2(x3),Base3(x4)
        {
            cout<<"Derived constructor called "<<x1<<endl;
        }
        ~Derived()
        {
            cout<<"Derived destructor called"<<endl;
        }
    };
    
    Base1::Base1(int x)
    {
        cout<<"Base1 constructor called "<<x<<endl;
    }
    
    Base1::~Base1()
    {
        cout<<"Base1 destructor called"<<endl;
    }
    
    Base2::Base2(int x)
    {
        cout<<"Base2 constructor called "<<x<<endl;
    }
    
    Base2::~Base2()
    {
        cout<<"Base2 destructor called"<<endl;
    }
    
    Base3::Base3(int x)
    {
        cout<<"Base3 constructor called "<<x<<endl;
    }
    
    Base3::~Base3()
    {
        cout<<"Base3 destructor called"<<endl;
    }
    
    int main()
    {
        int x[4];
        for (int i = 0; i < 4; ++i)
            cin >> x[i];
        Derived d(x[0], x[1], x[2], x[3]);
        return 0;
    }
  • 相关阅读:
    python 单例模式
    socketserver 多进程、多线程应用实例
    socket 编程的一些应用例子
    模拟一个http 请求的json格式报文,带 rsa 签名操作
    python excel基本操作
    多线程 进程间共享变量等
    多线程 multiprocessing 的几个小例子
    mysql 数据库的相关操作
    正则表达式匹配IP地址
    32-服务的容量规划:怎样才能做到有备无患
  • 原文地址:https://www.cnblogs.com/Konayuki2015/p/4520489.html
Copyright © 2011-2022 走看看