BJFUOJ-C++程序设计-实验3-继承和虚函数

ops/2024/9/23 5:23:08/

A TableTennisPlayer

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

答案:

#include<iostream>
#include<cstring>
using namespace std;class TableTennisPlayer{
private:string firstname;string lastname;bool hasTable;public:TableTennisPlayer(const string &, const string &, bool);string FirstName() const;string LastName() const;bool HasTable() const;
};TableTennisPlayer::TableTennisPlayer(const string & fn, const string & ln, bool h)
{firstname=fn;lastname=ln;hasTable=h;
}string TableTennisPlayer::FirstName()const{ return firstname;
}string TableTennisPlayer::LastName()const{ return lastname;
}bool TableTennisPlayer::HasTable()const{return hasTable;
}class RatedPlayer :public TableTennisPlayer{
private:int rating;
public:RatedPlayer(int,const string &, const string &, bool);int Rating() const;
};RatedPlayer::RatedPlayer(int r,const string & fn, const string & ln, bool h):TableTennisPlayer(fn,ln,h)
{rating=r;
}int RatedPlayer::Rating() const
{return rating;
}int main(){string firstname, lastname;bool hasTable;int rating;char flag;while(cin>>flag){if(flag=='T'){cin>>firstname>>lastname>>hasTable;TableTennisPlayer tp(firstname,lastname,hasTable);if(tp.HasTable())cout<<tp.FirstName()<<" "<<tp.LastName()<<" has a table.\n";elsecout<<tp.FirstName()<<" "<<tp.LastName()<<" hasn't a table.\n";} else if(flag=='R'){		cin>>firstname>>lastname>>hasTable>>rating;RatedPlayer rp(rating,firstname,lastname,hasTable);if(rp.HasTable())cout<<rp.FirstName()<<" "<<rp.LastName()<<" has a table. The rating is "<<rp.Rating()<<".\n";elsecout<<rp.FirstName()<<" "<<rp.LastName()<<" hasn't a table. The rating is "<<rp.Rating()<<".\n";}}return 0;}

重要知识点:

·继承相关知识;
·派生类对象构造函数先调用父类构造函数,以及如何实现。

B Person和Student

在这里插入图片描述
在这里插入图片描述

答案:

#include<iostream>
#include<cstring>using namespace std;class Person{
private:string name;
public:virtual void input();virtual void display();
};void Person::input()
{cin>>name;
}void Person::display()
{cout<<name<<endl;
}class Student:public Person{
private:string id;
public:void input();void display();
};void Student::input()
{cin>>id;Person::input();
}void Student::display()
{cout<<id<<" ";Person::display();
}int main(){Person * p;p = new Person;p->input();p->display();delete p;p = new Student;p->input();p->display();delete p;return 0;
}

重要知识点:

·多态(主函数中体现)
·虚函数的定义与在子类中的实现(virtual关键字)
·赋值兼容规则是指在公有继承情况下,对于某些场合,一个派生类的对象可以
作为基类对象来使用。即:公有派生类的对象可以赋值给一个基类对象,并自
动转换为基类类型。 但基类对象只能“看到”其所替代的派生类对象中的基类部分。

C 图书商品

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

答案:

#include<iostream>using namespace std;class Item_base //未打折的图书商品
{
protected:string ISBN; //图书序列号double price; //单价public:Item_base(const string & book_ISBN = "", double sales_price = 0.0);string get_ISBN() const;virtual double net_price(int) const; //返回购买指定数量的图书的总价virtual ~Item_base();
};Item_base::Item_base(const string & book_ISBN, double sales_price)
{ISBN=book_ISBN;price=sales_price;
}string Item_base::get_ISBN()const
{return ISBN;
}double Item_base::net_price(int n) const
{return price*n;
}Item_base::~Item_base(){
}class Bulk_Item : public Item_base //根据购买数量打折
{
public:Bulk_Item(const string & book_ISBN = "", double sales_price = 0.0, int min_qty = 0, double discount = 0.0);double net_price(int) const; //返回根据购买数量打折后的总价
private:int min_qty; // 买够这个数量可以打相应的折扣double discount; //折扣
};Bulk_Item::Bulk_Item(const string & book_ISBN, double sales_price, int min_qty, double discount):Item_base(book_ISBN,sales_price)
{this->min_qty=min_qty;this->discount=discount;
}double Bulk_Item::net_price(int n)const
{return price*n*(1.0-discount);
}int main()
{Item_base book("0-001-0001-1", 10.0);Bulk_Item bulk1("0-001-0001-1",10.0, 5, 0.1);Bulk_Item bulk2("0-001-0001-1", 10.0, 10, 0.2);int num;while (cin >> num){cout << bulk1.get_ISBN() << "\t" << num << "\t";Item_base * p;if (num >= 10) p = &bulk2;else if (num >= 5) p = &bulk1;else p = &book;cout << p->net_price(num) << "\n";}return 0;
}

重要知识点

·虚函数:
(1)只有类的普通成员函数或析构函数才能声明为虚函数。
(2)基类中的虚函数可以在派生类中被重新定义,但重新定义时必须与基类中的函数原型完全相同(同名覆盖),且无论是否用virtual修饰,系统都将其视为虚函数(建议加上virtual)。
(3)当一个类的公有成员函数声明为虚函数,且在其公有派生类中被同名覆盖时:

 当用基类指针(或引用)指向这个派生类对象,并用该指针调用该函数时,系统自动用派生类中
的同名函数。

 即:当用基类指针(或引用)指向派生类对象,并用该指针调用该函数时,系统会在程序运行中根据所指向对象的不同,自动选择执行当前指向对象所属类的成员函数,从而实现了运行时的多
态性。
(5)静态成员函数不能声明为虚函数。因为静态成员函数不属于某一个对象,没
有多态性的特征。
(6)构造函数不能是虚函数。
(7)析构函数可以是虚函数,且往往被声明为虚函数。

D Vehicle类

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

答案:

#include<iostream>using namespace std;class Vehicle{
protected:string name;string color;
public:Vehicle(string ,string);virtual void display()=0;
};Vehicle::Vehicle(string n,string c)
{name=n;color=c;
}class Car:public Vehicle{
private: int passenger;
public:Car(string,string,int);void display();
};Car::Car(string n,string c,int num):Vehicle(n,c)
{passenger=num;
}void Car::display()
{cout<<"Car name:"<<name<<" Car color:"<<color<<" Car passenger:"<<passenger<<endl;
}class Truck:public Vehicle{
private: double capacity;
public:Truck(string,string,double);void display();
};Truck::Truck(string n,string c,double m):Vehicle(n,c)
{capacity=m;
}void Truck::display()
{cout<<"Truck name:"<<name<<" Truck color:"<<color<<" Truck capacity:"<<capacity<<endl;
}int main()
{Vehicle *p;char type;char name[110],color[110];int pas;double cap;while(cin>>type){cin>>name>>color;if(type == 'C'){cin>>pas;Car car(name,color,pas);p = &car;p->display();}else if(type == 'T'){cin>>cap;Truck truck(name,color,cap);p = &truck;p->display();}}
return 0;
}

重要知识点:

·纯虚函数:
在定义一个表达抽象概念的基类时,有时可能会无法给出某些成员
函数的具体实现。这时,就可以将这些函数声明为纯虚函数。

virtual 类型 函数名(参数表)=0 ;

 即纯虚函数就是在基类中声明为虚函数,但未给出具体函数定义体的函数

 在各派生类中对该虚函数进行同名覆盖(即重写)。

·抽象类:
类体内声明了纯虚函数的类,称为抽象类。

 抽象类的主要作用:以此类为基类建立的一个类族具有一组公共的接口(即公
有成员函数),将它们定义为纯虚函数,使它们能够更有效地发挥多态特性。

 抽象类声明了一族派生类的共同接口,而接口的完整实现,即纯虚函数的函数

体,要由派生类自己定义。

·使用纯虚函数与抽象类的注意事项:
(1)抽象类只能用作基类来派生出新类,不能声明抽象类的对象,但可以声明指向抽象类的指针
变量或引用变量。

(2)抽象类中可以有多个纯虚函数。

(3)抽象类中也可以定义其他非纯虚函数。

(4)抽象类派生出新类之后,如果在派生类中没有重新定义基类中的纯虚函数,则必须再将该虚
函数继续声明为纯虚函数,此时,这个派生类仍然是一个抽象类;

(5)在一个复杂的类继承结构中,越上层的类抽象程度就越高,有时甚至无法给出某些成员函数
的具体实现。
(6)引入抽象类的目的主要是为了能将相关类组织在一个类继承结构中,并通过抽象类来为这些
相关类提供统一的操作接口。

E 表面积和体积

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

答案:

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;const double pi = acos(-1);class Shape {
public:
Shape() {}
virtual double area() = 0;
virtual void input() = 0;
virtual double volume() = 0;
virtual ~Shape() {}
};class Cylinder:public Shape
{
private:int r;int h;
public:double area();void input();double volume();
};double Cylinder::area()
{return pi*r*r*2+2*pi*r*h;
}void Cylinder::input()
{cin>>r>>h;
}double Cylinder::volume()
{return pi*r*r*h;
}class Cuboid:public Shape
{
private:int a;int b;int c;
public:double area();void input();double volume();
};double Cuboid::area()
{return 2*a*b+2*a*c+2*b*c;
}void Cuboid::input()
{cin>>a>>b>>c;
}double Cuboid::volume()
{return a*b*c;
}class Ball:public Shape
{
private:int r;
public:double area();void input();double volume();
};double Ball::area()
{return 4.0*pi*r*r;
}void Ball::input()
{cin>>r;
}double Ball::volume()
{return (4.0/3.0)*pi*r*r*r;
}void work(Shape *s) {s->input();cout << s->area() << " " << s->volume() << endl;delete s;
}int main() {char c;while (cin >> c) {switch (c) {case 'y':work(new Cylinder());break;case 'c':work(new Cuboid());break;case 'q':work(new Ball());break;default:break;}}return 0;
}

重要知识点:

同上


http://www.ppmy.cn/ops/30252.html

相关文章

探索高级聚类技术:使用LLM进行客户细分

在数据科学领域&#xff0c;客户细分是理解和分析客户群体的重要步骤。最近&#xff0c;我发现了一个名为“Clustering with LLM”的GitHub仓库&#xff0c;它由Damian Gil Gonzalez创建&#xff0c;专门针对这一领域提供了一些先进的聚类技术。在这篇文章中&#xff0c;我将概…

cookie、session、token

cookie 纳入标准文档&#xff0c;标准浏览器需要遵守的协议之一&#xff0c;作为标准浏览器必须支持的。 WEB应用都是基于HTTP协议&#xff0c;标准的HTTP协议是无状态的。 什么是无状态&#xff1f; 不管是谁&#xff0c;不管是从哪个地方发起的请求。只要你的请求&#xff08…

数据结构——循环结构:for循环

今天是星期五&#xff0c;明天休息&#xff0c;后天补课&#xff0c;然后就是运动会&#xff0c;接着是放假。&#xff08;但这些都和我没关系啊&#xff0c;哭死&#xff01;&#xff09;今天脑袋难得清醒一会儿&#xff0c;主要是醒的比较早吧&#xff0c;早起学了一会&#…

Baumer工业相机堡盟工业相机如何联合OpenHarmony框架开发连接USB相机(OpenHarmony)

Baumer工业相机堡盟工业相机如何联合OpenHarmony框架开发连接USB相机&#xff08;OpenHarmony&#xff09; Baumer工业相机介绍OpenHarmony介绍使用OpenHarmony开发连接Baumer工业USB相机1.配置权限2.初始化相机功能3.使用USB相机采集图像4.使用USB相机保存图像5.释放相机资源 …

unity3d使用3D WebView播放网页中的视频

Unity2021.3.35f1&#xff0c;硬件ESP32-Cam&#xff0c;3D WebView插件 1.新建工程&#xff0c;导入3D WebView for Winfows和3D WebView for Android 2.打开场景Assets\Vuplex\WebView\Demos\Scenes\2_CanvasWebViewDemo 3.修改Canvas的Render Mode为Screen Space-Camera&am…

【c语言实现内核链表】

在C语言中实现内核链表可以参考以下步骤&#xff1a; 定义链表节点结构&#xff1a;创建一个表示链表节点的结构体&#xff0c;通常包含一个数据成员和一个指向下一个节点的指针。 struct ListNode {// 数据成员int data;// 指向下一个节点的指针struct ListNode* next; };初…

flutter、kotlin、java中将方法作为入参的区别

1、带参数带返回值的方法作为入参 &#xff08;1&#xff09;flutter 定义 int add(int a, int b) > a b; int subtract(a, b) > a - b;performOperation(int a, int b, int Function(int, int) function) > function(a, b);// //简化写法 // add(a, b) > a b…

旅游新策略,共享与补贴助力地方经济繁荣

在当前的经济环境中&#xff0c;旅游业对于地方经济增长的重要性日益凸显。各个城市都在积极探索增加旅游流量的方法&#xff0c;以刺激本地经济的增长。 例如&#xff0c;淄博政府通过政策推动和合作模式&#xff0c;成功吸引了大量游客&#xff0c;这成为了一个成功的案例。…