C++语言程序设计进阶(学堂在线)习题

一系列C++编程题目,涉及类的设计、多继承、运算符重载、模板函数及异常处理等概念,旨在提升对C++类和继承的理解与应用能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C7-1 账户类 (100 满分)
题目描述
定义一个基类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(){
   };
    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()函数
Account::Account(string name)
{
   
    userName = name;
}

void Account::PrintUserName()
{
   
    cout << userName << endl;
}

CreditAccount::CreditAccount(string name,int number):Account(name),credit(number){
   }

void CreditAccount::PrintInfo()
{
   
    PrintUserName();
    cout << credit << endl;
}


int main()
{
   
    CreditAccount a("I Love CPP", 10000);
    a.PrintInfo();
    return 0;
}

C7-2 多继承 (100 满分)
题目描述

下面的代码声明了三个基类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,Base1,Base3//继承上面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;

}

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;

}

Derived::Derived(int x1,int x2,int x3,int x4):Base2(x3),Base1(x2),Base3(x4)
{
   
    cout << "Derived constructor called " << x1 << endl;
}

Derived::~Derived()
{
   
    cout << "Derived 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;
}

C7-3 用类实现A+B (100 满分)
题目描述
下面的代码声明了两个基类Base1和Base2,然后从这两个基类按照公有方式派生出类Derived。基类和派生类都各自包含一个公有成员x,并且Base1和Base2各有接受一个整型参数的构造函数,Derived的构造函数接受Base1和Base2的对象引用a,b来初始化Derived类对象,并令x为Base1::x和Base2::x之和。请将下面的代码补充完成,使得输出符合要求。

#include
using namespace std;

struct Base1
{
int x;
Base1(int x);
};

struct Base2
{
int x;
Base2(int x);
};

struct Derived:public Base1, public Base2
{
int x;
Derived(Base1& a, Base2& b);
};

//请实现Base1,Base2, Derived的构造函数

int main()
{
int x, y;
cin >> x >> y;
Base1 a(x);
Base2 b(y);
Derived d(a, b);
cout << d.Base1::x << “+” << d.Base2::x << “=” << d.x << endl;
return 0;
}

输入描述
每组输入为 2 个整数用空格隔开

输出描述
主函数自动完成输出

样例输入
1 2

样例输出
1+2=3

#include <iostream>
using namespace std;
 
struct Base1
{
   
    int x;
    Base1(int x);
};
 
struct Base2
{
   
    int x;
    Base2(int x);
};
 
struct Derived:public Base1, public Base2
{
   
    int x;
    Derived(Base1& a, Base2& b);
};

//请实现Base1,Base2, Derived的构造函数
Base1::Base1(int x)
{
   
    this->x = x;
}

Base2::Base2(int x)
{
    
   this->x = x;
}

Derived::Derived(Base1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值