Xi'an Petroleum University C++ computer experiment on computer III: Inheritance and derived programming (2 credit hours)

Hands-on 3: Inheritance and Derivation Programming (2 credit hours)

Purpose

Learn about the important role of inheritance in object-oriented programming.

Understand the concepts of inheritance and derivation.

Master the method of deriving a new class through inheritance.

Master the function and usage of virtual base class.

Experimental content

P169: 5.19, 5.22

On-board content

Design a basic account class first, and then design a savings account class by inheriting the basic account class.
Member variables such as password, address, minimum balance and interest rate are added to the savings account class, and some
member functions often used by bank accounts are added. Require:

(1) The member functions in the class have the function of inputting and outputting the above information of depositors;

(2) Design the account to be unchangeable;

(3) The original password must be provided when changing the password.

#include<string>
#include<iostream>
using namespace std;
class Bank
{
    
    
private:
30
31
char name[20];//姓名
 long id;//账号
long balance;//余额
public:
Bank(char *Name,long Id,long Balance)//const变量需要用初始化表初始化
{
    
    
strcpy(name,Name);
id=Id;
balance=Balance;
}
string GetName(){
    
    return name;}//获取姓名
long GetId(){
    
    return id;}//获取账号
long GetBalance(){
    
    return balance;}//获取余额
long cunkuan(long money)//存款
{
    
    
balance+=money;
return balance;
}
long qukuan(long money)//取款
{
    
    
balance-=money;
return balance;
}
 
string change_name()//修改姓名
{
    
    
 char new_name[20];
 cout<<"请输入修改后的姓名:"<<endl;
 cin>>new_name;
 strcpy(name,new_name);
 cout<<"修改成功!"<<endl;
 return name;
}
};
class SaveBank:public Bank
{
    
    
private:
char password[20];//密码
char address[20];//住址
long minimum_balance;//最小余额
32
float annual_interest_rate;//年利率
public:
SaveBank(char *Name,long Id,long Balance,char *Password,char 
*Address,long Minimum_balance,float 
Annual_interest_rate):Bank(Name,Id,Balance),minimum_balance(Minimum_balance)
,annual_interest_rate(Annual_interest_rate)
{
    
    
strcpy(password,Password);
strcpy(address,Address);
}
string GetPassword(){
    
    return password;}//获取密码
 string GetAddress(){
    
    return address;}//获取住址
long Get_minimum_balance(){
    
    return minimum_balance;}//获取最小余额
float Get_annual_interest_rate(){
    
    return annual_interest_rate;}//获取年利率
string change_password()//修改密码
{
    
    
 char new_password[20];
 char number[20];
cout<<"请输入原始密码:"<<endl;
cin>>number;
if((strcmp(number,password))==0)
{
    
    cout<<"请输入新密码:"<<endl;
cin>>new_password;
strcpy(password,new_password);
cout<<"修改成功!"<<endl;
}
else
{
    
    cerr<<"原始密码错误!请重新选择操作!"<<endl;}
return password;
}
string change_address()//修改地址
{
    
    
 char new_address[20];
 cout<<"请输入修改后的地址:"<<endl;
 cin>>new_address;
strcpy(address,new_address);
 cout<<"修改成功!"<<endl;
33
 return address;
}
void getbank()//获取客户账户信息
{
    
    
cout<<"姓名:"<<GetName()<<endl;
cout<<"账号:"<<GetId()<<endl;
cout<<"余额:"<<GetBalance()<<endl;
cout<<"密码:"<<GetPassword()<<endl;
cout<<"地址:"<<GetAddress()<<endl;
cout<<"最小余额:"<<Get_minimum_balance()<<endl;
cout<<"利率:"<<Get_annual_interest_rate()<<endl;
}
};4)添加源文件代码。在 bank.cpp 文件中添加主程序代码,如图 6 所示。
代码如下:
#include "stdafx.h"
#include<iostream>
#include<stdlib.h>
#include <stdio.h>
#include "bank.h"
using namespace std;
int main(void)
{
    
    
char name[20];
long id;
long balance;
char password[20];
char address[20];
long minimum_balance;
float annual_interest_rate;
int op; //操作
long money; //存款 or 取款数额
cout<<"请输入客户信息"<<endl;
cout<<"————————————————————————————————
————————"<<endl;
cout<<"姓名:";
cin>>name;
cout<<"账号:";
cin>>id;
cout<<"余额:";
cin>>balance;
cout<<"密码:";
35
cin>>password;
cout<<"地址:";
cin>>address;
cout<<"最小余额:";
cin>>minimum_balance;
cout<<"年利率:";
cin>>annual_interest_rate;
SaveBank 
jianghan(name,id,balance,password,address,minimum_balance,annual_interest_rate);
cout<<"———————————请输入操作
———————————————————————"<<endl;
cout<<"1:打印客户信息"<<endl;
cout<<"2:存款"<<endl;
cout<<"3:取款"<<endl;
cout<<"4:修改密码"<<endl;
 cout<<"5:修改姓名"<<endl;
 cout<<"4:修改地址"<<endl;
while(cin>>op)
{
    
    
switch(op)
{
    
    
case 1:
jianghan.getbank();
break;
case 2:
cout<<"请输入存款金额:"<<endl;
cin>>money;
jianghan.cunkuan(money);
cout<<"当前余额:"<<jianghan.GetBalance()<<endl;
break;
case 3:
cout<<"请输入取款金额:"<<endl;
cin>>money;
jianghan.qukuan(money);
cout<<"当前余额:"<<jianghan.GetBalance()<<endl;
break;
case 4:
jianghan.change_password();
break;
case 5:
36
jianghan.change_name();
break;
case 6:
jianghan.change_address();
break;
default:
cerr<<"操作错误!"<<endl;
}
}
return 0;
}

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-W7YJkxMd-1681461509616)(2023-04-14-16-15-51.png)]

Experimental content

P169: 5.19, 5.22

5.19 Design an animal class, which contains some animal attributes, such as name, size, weight, etc. Animals can run or walk. Then design a bird, in addition to the basic attributes of animals, it has its own feathers, wings, etc. In addition to running or walking, a bird can also fly. In order to inherit the properties of the Animal class, Birds should inherit from the Animal class. Write a program to test whether the designed bird can achieve the intended function.

#include <iostream>
#include<string>
using namespace std;
class Animal //声明基类Animal
{
    
    public:
Animal(string n,char s,double w) //基类构造函数
{
    
    
name=n;
size=s;
weight=w;
}
~Animal( ){
    
     } //基类析构函数
void Run();
void Walk();
protected: //私有部分
string name;
char size;
double weight;
};
class Bird: public Animal //声明派生类Animal
{
    
    public:
Bird(string n,char s,double w,string f,string wi):Animal(n,s,w)
{
    
    
feather=f; //在函数体中只对派生类新增的数据成员初始化
wing=wi;
}
~Bird( ){
    
     } //派生类析构函数
void show( );
void Fly();
private: //派生类的私有部分
string feather;
string wing;
};
void Animal::Run()
{
    
    
cout<<"I can run!"<<endl;
}
void Animal::Walk()
{
    
    
cout<<"I can walk!"<<endl;
}
void Bird::show()
{
    
    
cout<<"name:"<<name<<endl;
cout<<"size:"<<size<<endl;
cout<<"weight:"<<weight<<endl;
cout<<"feather:"<<feather<<endl;
cout<<"wing:"<<wing<<endl;
}
void Bird::Fly()
{
    
    
cout<<"I can fly!"<<endl;
}
void main()
{
    
    
Bird bird("乌鸦",'L',50,"黑色羽毛","斑点翅膀");
bird.show ();
bird.Run ();
bird.Walk ();
bird.Fly ();
}
//我是这样的
public class Animal{
    
    
  private String name;
  private double weight;
  private int length;
  privateString sex;
  public void run(){
    
    
  System.out.println("the animal is running!");来源:考试大
  }
  public void jump(){
    
    
  System.out.println("the animal is jumping!");
  }
  public void walk(){
    
    
  System.out.println("the animal is walking!");
  }
  }

c++ manage zoo animals

Problem Description

动物园(Zoo)有三种动物(Animal):猫(Cat)、狗(Dog)、老虎(Tiger),显示所有动物的信息。
(1)定义5个类,动物园(Zoo)、动物(Animal)、猫(Cat)、狗(Dog)、老虎(Tiger)。
Animal是抽象基类,Cat、Dog是Animal的派生类,Tiger是Cat的派生类。
Animal中有2个数据成员。一个是动物的姓名,另一个是动物的年龄。有1个成员函数,是纯虚函数void Display(),用来显示动物的姓名和年龄。
Cat、Dog和Tiger中有3个成员函数。构造函数、析构函数和重写的函数void Display()。
Zoo中有3个数据成员。分别是:动物园最多能容纳的动物数(max)、动物园当前动物数(num)、指向Animal的数组(Animal** asp)。
Zoo中有4个成员函数。分别是:
构造函数(Zoo(int max)),完成这几个任务,把构造函数参数的值赋给对象的max,动物园当前动物数为0,
new一个Animal的数组并且都初始化为NULL。
析构函数,释放数组空间。
添加动物函数(void Add(Animal* ap)),把ap指向的动物添加到动物园数组中。
显示动物园所有动物信息函数(void ShowAll())。
(2)在main函数中创建1个最大数为5的动物园,再创建2个猫,2个狗,1个老虎,然后把它们添加到动物园,最后显示所有动物的信息。
请完成下面的程序:
————————————————
版权声明:本文为CSDN博主「爱吃月饼的兔」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/dimrhyme/article/details/121961164
int main()
{
    
    
Zoo z(5);
Animal* pa = new Cat (“AAA”, 3);
z.Add(pa);
//delete pa;
pa = new Dog(“BBB”, 1);
z.Add(pa);
//delete pa;
pa = new Tiger (“CCC”, 2);
z.Add(pa);
//delete pa;
z.ShowAll();
return 0;
}

Sample Output
Cat:AAA,3
Dog:BBB,1
Tiger:CCC,2

#include <iostream>
using namespace std;
#include <string>


class Animal
{
    
    
public:
	string name;
	int age;
	virtual void Display() = 0;
};

class Cat :public Animal
{
    
    
public:
	Cat(){
    
    }
	Cat(string a, int b)
	{
    
    
		name = a;
		age = b;
	}
	void Display()
	{
    
    
		cout << "Cat:" << name << "," << age << endl;
	}
	~Cat(){
    
    }
};

class Dog : public Animal
{
    
    
public:
	Dog(string a, int b)
	{
    
    
		name = a;
		age = b;
	}
	void Display()
	{
    
    
		cout << "Dog:" << name << "," << age << endl;
	}
	~Dog(){
    
    }
};

class Tiger :public Cat
{
    
    
public:
	//Tiger(string a, int b):Cat(a, b){}  这种不用在父类中构造无参构造函数
	Tiger(string a, int b)//此法需要在Cat中构造无参构造函数
	{
    
    
		name = a;
		age = b;
	}
	void Display()
	{
    
    
		cout << "Tiger:" << name << "," << age << endl;
	}
	~Tiger(){
    
    }
};

class Zoo//此类放在最后
{
    
    
public:
	int max;//最大动物数
	static int num;//动物数
	Animal** asp;//指针
	Zoo(int a)
	{
    
    
		max = a;
		asp = new Animal * [max];//动物数组 asp是指针
	}
	void Add(Animal* ap)//添加动物函数
	{
    
    
		if (num < max)
		{
    
    
			*(asp + num) = ap;
			num++;//计数
		}
	}
	void ShowAll()
	{
    
    
		for (int i = 0; i < num; i++)
		{
    
    
			asp[i]->Display();
		}
	}
	~Zoo()
	{
    
    
		delete asp;//释放堆区创建的空间
	}
};
int Zoo::num = 0;


int main()
{
    
    
	Zoo z(5);
	Animal* pa = new Cat("AAA", 3);
	z.Add(pa);
	//delete pa; 
	pa = new Dog("BBB", 1);
	z.Add(pa);
	//delete pa; 
	pa = new Tiger("CCC", 2);
	z.Add(pa);
	//delete pa; 
	z.ShowAll();
	return 0;
}

————————————————
版权声明:本文为CSDN博主「爱吃月饼的兔」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/dimrhyme/article/details/121961164

5.21 First design a basic account class, then design a savings account class by inheriting the basic account class, add a static member variable (annual interest rate) to the savings account class, and add the following member functions:

(1) Calculate monthly interest

Monthly interest = deposit amount × annual interest rate ÷ 12

(2) Change the interest rate (static method): reset the annual interest rate

Finally, write a program to test whether the designed savings account class can achieve the intended function.

#include<iostream>
#include<string.h>
using namespace std;

class basic {
    
    
public:
	long id;
	basic(long a):id(a)						//基类构造函数
	{
    
    }
	void show(long id) {
    
    
		cout <<"账号:" << id << endl;
	}
	~basic()								//基类析构函数
	{
    
    }
};
class account :private basic {
    
    
public:
	char name[20];
	long balance;
	char password[20];
	char address[20];
	long minimum_balance;
	float annual_interest_rate;
	account(char *a, long b, long c, char *d, char *e, long f, float g): //派生类构造函数
		basic(b), balance(c), minimum_balance(f), annual_interest_rate(g)
	{
    
    
		strcpy_s(name, a);
		strcpy_s(password, d);
		strcpy_s(address, e);
	}
	void show1();							//声明显示函数
	void deposit(long money);				//声明存款函数
	void withdrawal(long money);			//声明取款函数
	void change_password(char* chpassword);	//声明修改密码函数
	void modify_name(char* chname);			//声明修改姓名函数
	void modification_address(char* chaddress);//声明修改地址函数
	~account()								//派生类析构函数
	{
    
    }
};
	void account::show1()
	{
    
    
	cout << "姓名:" << name << endl;
	basic::show(id);
	cout << "余额:" << balance << endl;
	cout << "密码:" << password << endl;
	cout << "地址:" << address << endl;
	cout << "最小余额:" << minimum_balance << endl;
	cout << "年利率:" << annual_interest_rate << endl;
	}
	void account::deposit(long money) {
    
    
		balance += money;
	}
	void account::withdrawal(long money) {
    
    
		balance -= money;
	}
	void account::change_password(char *chpassword) {
    
    
		strcpy_s(password, chpassword);
	}
	void account::modify_name(char* chname) {
    
    
		strcpy_s(name, chname);
	}
	void account::modification_address(char* chaddress) {
    
    
		strcpy_s(address, chaddress);
	}

int main() {
    
    
	char name[20];
	long id;
	long balance;
	char password[20];
	char address[20];
	long minimum_balance;
	float annual_interest_rate;
	long money;						//存款或取款数额
	int a;

	cout << "请输入客户信息" << endl;
	cout << "---------------------------------" << endl;
	cout << "姓名:";
	cin >> name;
	cout << "账号:";
	cin >> id;
	cout << "余额:";
	cin >> balance;
	cout << "密码:";
	cin >> password;
	cout << "地址:";
	cin >> address;
	cout << "最小余额:";
	cin >> minimum_balance;
	cout << "年利率";
	cin >> annual_interest_rate;
	account n(name, id, balance, password, address, minimum_balance, annual_interest_rate);
	cout << "------------------------请输入操作------------------------------" << endl;
	cout << "1.打印客户信息" << endl << "2.存款" << endl << "3.取款" << endl; 
	cout << "4.修改密码" << endl <<"5.修改姓名" << endl << "6.修改地址" << endl;

	while (scanf("%d", &a) != EOF) {
    
    
		switch (a) {
    
    
		case 1:
			n.show1();
			break;
		case 2:
			cout << "请输入存款金额:" << endl;
			cin >> money;
			n.deposit(money);
			cout << "当前余额:" << n.balance <<endl;
			break;
		case 3:
			cout << "请输入取款金额:" << endl;
			cin >> money;
			n.withdrawal(money);
			cout << "当前余额:" << n.balance << endl;
			break;
		case 4:
			char chpassword[20];
			cout << "请输入原始密码:" << endl;
			cin >> chpassword;
			if (strcmp(n.password, chpassword) != 0) {
    
    
				cout << "原始密码错误!请重新选择操作!" << endl;
			}
			else {
    
    
				cout << "请输入新密码:" << endl;
				cin >> chpassword;
				n.change_password(chpassword);
				cout << "修改成功!" << endl;
			}
			break;
		case 5:
			char chname[20];
			cout << "请输入修改后的姓名:" << endl;
			cin >> chname;
			n.modify_name(chname);
			cout << "修改成功!" << endl;
			break;
		case 6:
			char chaddress[20];
			cout << "请输入修改后的地址:" << endl;
			cin >> chaddress;
			n.modification_address(chaddress);
			cout << "修改成功!" << endl;
			break;
		}
	}
	return 0;

insert image description here

thinking questions

Can you add a function to the savings account class: If the user needs to change the password, the old password must be provided.

* Inheritance of after-school exercises

 5.18 设计一个点类,它仅包含两个属性:横坐标和纵坐标。通过继承点类再设计一个圆类,它除了有一个圆心外,还有半径,还应该能够计算圆的周长和面积等。编写一个测试程序来测试所设计的类能否实现预定的功能。

 5.19 设计一个动物类,它包含一些动物的属性,如名称、大小、重量等,动物可以跑或走。然后设计一个鸟类,除了动物的基本属性外,它还有自己的羽毛、翅膀等。鸟除了跑或走外,它还可以飞翔。为了继承动物类的特性,鸟类应该继承动物类。编写一个程序来测试所设计的鸟类能否实现预定的功能。

 5.20 先设计一个长方形类,再通过继承长方形类设计一个正方形类,正方形类中通过覆盖父类的成员函数得到一些新的功能。

  5.21 先设计一个基本账户类,再通过继承基本账户类设计一个储蓄账户类,储蓄账户类中增加一个静态成员变量(年利率),并增加如下成员函数:

  (1)计算月利息

  月利息=存款金额×年利率÷12

  (2)更改利率(静态方法):重新设置年利率
 最后,编写一个程序来测试所设计的储蓄账户类能否实现预定的功能。

 5.22先设计一个基本账户类,再通过继承基本账户类设计一个储蓄账户类,储蓄账户类中增加密码、地址、最小余额和利率等成员变量,并增加一些银行账户经常用到的成员函数。要求:

  (1)类中的成员函数具有输入、输出储户上述信息的功能;

  (2)将账号设计成不可更改;

  (3)修改密码时要提供原密码。

  5.23 在阅读5.6.1节内容的基础上,进一步分析图书馆的图书和杂志管理与借阅方式,设计一个基本符合图书馆实际工作方式的图书和杂志借阅系统。

Guess you like

Origin blog.csdn.net/shaozheng0503/article/details/130157203