总结:
①虚函数用virtual修饰,继承的时候,如果父类方法不用virtual修饰,则不会构成重写,依旧调用父类方法,加了virtual之后,就可以重写了。
②读取控制台输入cin>>a>>b; 在控制台可以在一行按空格进行区分比如 1 3就把1给了a,把3给了b
RatedPlayer::RatedPlayer(int Rating, const string &FirstName, const string &LastName, bool
HasTable) :TableTennisPlayer(FirstName, LastName, HasTable)
③注意: :TableTennisPlayer(FirstName, LastName, HasTable)表示继承父类的参数,这种适用于父类的变量是私有的情况
ratedplayer::ratedplayer(int r, stri`ng fn, string ln, bool ht):tabletennisplayer(fn,ln,ht)
//只需把父类的含参数构造放在初始化列表中
④利用构造函数给char数组赋值 strcpy(name,name1);
char name[110];
char color[110];
Car(char name1[], char color1[], int pas)
{
//name = name1;
strcpy(name,name1);
strcpy(color, color1);
this->pas = pas;
}
⑤求表面积与体积
const double pi = acos(-1);
圆柱体:
表面积: return 2*pi*r*r+2*pi*r*hight;
体积: return pi*r*r*hight;
球体:
表面积: return (4)*pi*r*r;
体积: return (double)((double)(4)/double(3))*pi*r*r*r;
1:TableTennisPlayer
描述:
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
class TableTennisPlayer {
private:
string firstname;
string lastname;
bool hasTable;
public:
TableTennisPlayer();
TableTennisPlayer(const string &, const string &, bool);
string FirstName() const;
string LastName() const;
bool HasTable() const;
};
class RatedPlayer :public TableTennisPlayer {
public :
int rating;
int Rating();
RatedPlayer(int Rating ,const string &, const string &, bool);
};
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";
else
cout << 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";
else
cout << rp.FirstName() << " " << rp.LastName() << " hasn't a table. The rating is " << rp.Rating() << ".\n";
}
}
return 0;
}
TableTennisPlayer::TableTennisPlayer()
{
}
TableTennisPlayer::TableTennisPlayer(const string & FirstName, const string & LastName, bool HasTable)
{
this->firstname = FirstName;
this->lastname = LastName;
this->hasTable = HasTable;
}
string TableTennisPlayer::FirstName() const
{
return this->firstname;
}
string TableTennisPlayer::LastName() const
{
return this->lastname;
}
bool TableTennisPlayer::HasTable() const
{
return this->hasTable;
}
int RatedPlayer::Rating()
{
return this->rating;
}
RatedPlayer::RatedPlayer(int Rating, const string &FirstName, const string &LastName, bool HasTable) :TableTennisPlayer(FirstName, LastName, HasTable)
{
this->rating = Rating;
}
参考:c++类的继承
2:Person和Student
#include <iostream>
using namespace std;
class Person{
public:
string name;
string id;
void input()
{
cin>>name;
//scanf("%s %s\n",&id,&name);
cin>>id>>name;
}
void display()
{
cout<<name<<endl;
cout<<id<<" "<<name<<endl;
}
};
class Student: public Person{
public:
string name;
string id;
void input()
{
cin>>name;
cin>>id>>name;
}
void display()
{
cout<<name<<endl;
cout<<id<<" "<<name<<endl;
}
};
int main()
{
Person * p;
p = new Person;
p->input();
p->display();
delete p;
p = new Student;
p->input();
p->display();
delete p;
return 0;
}
3:图书商品
#include <iostream>
#include <string>
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();
};
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; //折扣
};
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;
}
Bulk_Item::Bulk_Item(const string & book_ISBN, double sales_price, int min_qty, double discount)
{
this->ISBN = book_ISBN;
this->price = sales_price;
this->min_qty = min_qty;
this->discount = discount;
}
double Bulk_Item::net_price(int num) const
{
return price * num-this->discount*this->price*num;
}
Item_base::Item_base(const string & book_ISBN, double sales_price)
{
this->ISBN = book_ISBN;
this->price = sales_price;
}
string Item_base::get_ISBN() const
{
return ISBN;
}
double Item_base::net_price(int num) const
{
return num*price;
}
Item_base::~Item_base()
{
}
4:Vehicle类
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
class Vehicle {
public:
char name[110];
char color[110];
virtual void display()
{
}
};
class Car :public Vehicle {
public:
int pas;
Car(char name1[], char color1[], int pas)
{
//name = name1;
strcpy(name,name1);
strcpy(color, color1);
this->pas = pas;
}
//virtual ~Car();
virtual void display()
{
cout << "Car name:" << name << " Car color:" << color << " Car passenger:" << pas << endl;
}
};
class Truck :public Vehicle {
public:
double cap;
Truck(char name1[], char color1[], double cap)
{
//name = name1;
strcpy(name, name1);
strcpy(color, color1);
this->cap = cap;
}
virtual void display()
{
//cout<<1<<endl;
cout << "Truck name:" << this->name << " Truck color:" << this->color << " Truck capacity:" <<this->cap << 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;
// printf("%c",color[1]);
p->display();
}
}
return 0;
}
c++ error: ‘strcpy’ was not declared in this scope 解决方式
字符数组赋值报“表达式必须是可修改的左值”的错误
表达式必须是可修改的左值
c++继承父类的子类,如何调用父类的同名函数?
https://zhidao.baidu.com/question/49083630.html?qbl=relate_question_0&word=%D6%D8%D0%B4%B8%B8%C0%E0%B5%C4%BA%AF%CA%FD%B5%AB%CA%C7%BA%AF%CA%FD%B5%F7%D3%C3%B8%B8%C0%E0%B5%C4%B7%BD%B7%A8
5:求表面积和体积
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Shape {
public:
Shape() {}
virtual double area() = 0;
virtual void input() = 0;
virtual double volume() = 0;
virtual ~Shape() {}
};
void work(Shape *s) {
s->input();
cout << s->area() << " " << s->volume() << endl;
delete s;
}
class Cylinder : public Shape {
public:
const double pi = acos(-1);
int r, hight;
Cylinder() {};
void input() {
cin >> r >> hight;
}
double volume() {
return pi*r*r*hight;
}
double area() {
return 2*pi*r*r+2*pi*r*hight;
}
};
class Cuboid : public Shape {
public:
Cuboid() {};
int length, hight, width;
void input() {
cin >> length >> hight >> width;
}
double volume() {
return length * hight* width;
}
double area() {
return 2 * (length * hight + length * width + hight * width);
}
};
class Ball : public Shape {
public:
Ball() {};
const double pi = acos(-1);
int r;
void input() {
cin >> r;
}
double volume() {
return (double)((double)(4)/double(3))*pi*r*r*r;
}
double area() {
return (4)*pi*r*r;
}
};
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;
}
6:单继承
#include<iostream>
#include<cstring>
using namespace std;
class Base
{
private:
int n;
public:
Base(int a)
{
n = a;
}
void show()
{
cout << "n=" << n << endl;
}
};
class A :public Base
{
private:
int m;
public:
A(int a, int b):Base(a)//请实现该构造函数
{
// super(a,b);
m=b;
}
void showa()
{
show();
cout << "m=" << m << endl;
}
};
int main()
{
A dobj(10, 20);
dobj.showa();
return 0;
}
7:单继承-子对象
#include <iostream>
using namespace std;
class Base
{
private:
int n;
public:
Base(){
}
Base(int a)
{
n = a;
cout << "n=" << n << endl;
}
};
class A :public Base
{
private:
int m;
Base obj;
public:
A(int a, int b, int c) :Base(a)//请实现该构造函数
{
cout << "n=" <<c << endl;
cout << "m=" <<b << endl;
}
};
int main()
{
A dobj(10, 20, 30);
return 0;
}
8:虚基类
#include <iostream>
using namespace std;
class Base
{
protected:
int a;
public:
Base() {
}
Base(int x) :a(x)
{
}
Base(int x,int y) :a(x)
{
}
};
class Base1 : virtual public Base
{
protected:
int b;
public:
Base1() {
}
Base1(int x, int y) : Base(y), b(x)
{
}
};
class Base2 : virtual public Base
{
protected:
int c;
public:
Base2() {
}
Base2(int x, int y) : Base(y)
{
c = x;
}
};
class Derived : public Base1, public Base2
{
protected:
int d3;
public:
Derived(int x, int y); //请实现该函数
void show()
{
cout << "a=" << a << endl;
cout << "Base::a=" << Base::a << endl;
cout << "Base1::a=" << Base1::a << endl;
cout << "Base2::a=" << Base2::a << endl;
cout << "b=" << b << endl;
cout << "c=" << c << endl;
}
};
int main()
{
Derived dobj(10, 20);
dobj.show();
return 0;
}
Derived::Derived(int x, int y)
{
this->a=x+y;
this->b = x;
this->c = y;
}