C++复习

C++小复习

不知不觉已经到7.17号了呀,还有28天左右就要开学了!!!开学就开始考试了,所以,对于C++这门课也要开始复习了。幸亏老师讲的也不多,重点是类。类的构造方法,拷贝构造,虚函数,纯虚函数等等。对于MFC的知识点较少,只需要知道那几个响应函数就可以了。接下来就开始复习一下吧,顺便来总结一些C++类与Java类的区别。说多了都是泪,快忘完了,先来个HelloWorld程序回想起来。

一:HelloWorld程序

1
2
3
4
5
6
#include <iostream>
using namespace std;
int main(){
cout<<"hello word";
return 0;
}

注意:不要忘了写头文件:#include

​ 命名空间:using namespace std;

二:一些基础知识

1.基本数据类型

和java不同的是,C++的基本数据类型有关键字,eg:unsiged int(无符号整型);siged int(有符号整型)。

2.转义字符

\n:换行符

\t:水平制表符

\0:用于字符串结束的标志

3.符号常量

在C++里,符号常量的定义是使用const

在Java中,符号常量的定义是使用final

4.运算符与表达式

主要注意%:

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;
int main(){
cout<<-5%2<<endl;//结果:-1
cout<<5%2<<endl;//结果:1
cout<<5%-2<<endl;//结果:1
return 0;
}

条件运算符不要忘

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;
int main(){
int a=1,b=2,c;
c=(a<b)?3:1;
cout<<c;
return 0;
}

sizeof()运算符

1
2
3
4
5
6
#include <iostream>
using namespace std;
int main(){
cout<<sizeof(int);
return 0;
}

5.对于一些关于类的基础知识,在练习题中体现

练习题在下面

三:基本知识的使用

1.求阶乘

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
int main(){
//求阶乘
int n;
int sum=1;
cout<<"请输入一个数:"<<endl;
cin>>n;
for(int i=1;i<=n;i++){
sum=sum*i;
}
cout<<sum;
}

2.求水仙花个数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;
int main(){
int n;
int ge;
int shi;
int bai;
cout<<"请输入一个三位数:";
cin>>n;
if(n>=100&&n<=999){
ge=n%10;
shi=(n/10)%10;
bai=n/100;

if(n==ge*ge*ge+bai*bai*bai+shi*shi*shi){
cout<<n<<"是一个水仙花数"<<endl;
}else{
cout<<"该数不是"<<endl;
}

}else{
cout<<"请输入正确的三位数" ;
}
return 0;
}

四:类的创建

P72:课后题练习:

1.定义一个雇员类(姓名、工龄、薪水)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;
class Employees{
private:
char* name;
int salary;
int age;
public:
Employees(char* n,int a,int s){
name=n;
age=a;
salary=s;
}
Employees(){
}
void getNameAgeSa(){
cout<<"姓名:"<<name<<endl<<"年龄:"<<age<<endl<<"工薪:"<<salary<<endl;
;
}
};
int main(){
Employees employees1("刘畅",21,6000);
Employees employees2("王爽",21,6000);
employees1.getNameAgeSa();
employees2.getNameAgeSa();
return 0;
}
2.定义一个学生类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
class Student{
private:
char* name;
char* id;
char* sex;
int age;
public:
Student(char* n,char* i,char* s,int a){
name=n;
id=i;
sex=s;
age=a;
}
void getStudent(){
cout<<"姓名:"<<name<<endl<<"学号:"<<id<<endl<<"性别:"<<sex<<endl<<"年龄:"<<age;
}
};
int main(){
Student student1("刘畅","201852290302","男",21);
Student student2("王爽","201852232114","女",21);
student1.getStudent();
student2.getStudent();
return 0;
}
3.定义一个人员类Person,并派生出Student和Teacher类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;
class Person{
private:
char* name;
int id;
char* sex;
public:
Person(char* n,int i,char* s):name(n),id(i),sex(s){
}
void showPerson(){
cout<<"姓名:"<<name<<endl<<"编号:"<<id<<endl<<"性别:"<<sex<<endl;
}
};
class Student:public Person{
private:
int results;
public:
Student(char* n,int i,char* s,int r):Person(n,i,s),results(r){
}
void showStudent(){
cout<<"成绩:"<<results<<endl;
}
};
class Teacher:public Person{
private:
int age;
public:
Teacher(char* n,int i,char* s,int a):Person(n,i,s),age(a){
}
void showTeacher(){
cout<<"教龄:"<<age<<endl;
}
};
int main(){
Student student1("刘畅",001,"男",99);
Student student2("王爽",002,"女",100);
student1.showPerson();
student1.showStudent();
student2.showPerson();
student2.showStudent();
Teacher teacher1("郭伟",110,"男",10);
teacher1.showPerson();
teacher1.showTeacher();
return 0;
}
4.定义一个盒子类Box
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;
class Box{
private:
double width,height;
public:
Box(double w,double h):width(w),height(h){
}
void showBox(){
cout<<"宽度:"<<width<<endl<<"高度:"<<height<<endl;
}
};
class ColoredBox:public Box{
private:
char* color;
public:
ColoredBox(double w,double h,char* c):Box(w,h),color(c){
}
void showColoredBox(){
cout<<"颜色:"<<color<<endl;
}
};
int main(){
Box box(12,21);
box.showBox();
ColoredBox coloredBox(31,31,"red");
coloredBox.showBox();
coloredBox.showColoredBox();
return 0;
}
5.定义Student,子类Graduate

重点:拷贝构造函数的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;
class Student{
private:
double number,english,computer;
double average;
public:
Student(double n,double e,double c):number(n),english(e),computer(c){
}
void Getverage(){
average=(english+computer)/2.0;
}
void show(){
cout<<"学号:"<<number<<endl<<"平均成绩:"<<average<<endl;
}
};
class Graduate:public Student{
private:
char* teachAdviser;
public:
static int count;
Graduate(double n,double e,double c,char* t):Student(n,e,c),teachAdviser(t){
count++;
}
Graduate(Graduate &p):Student(p){
p.teachAdviser=teachAdviser;
}
void show(){
cout<<"导师:"<<teachAdviser<<endl;
}
static void ShowcountG(){
cout<<count<<endl;
}

};
int Graduate::count=0;
int main(){
Graduate graduate(001,90.0,80.0,"郭伟");
graduate.Getverage();
graduate.show();
graduate.ShowcountG();
Graduate graduate2(002,90,80,"郭伟");
graduate2.Getverage();
graduate2.show();
graduate2.ShowcountG();
return 0;
}
6.重载运算符

重点:运算符的重载

返回类型 operator+运算符(类名 对象名){}

对象调用:R1.operator+运算符(对象名);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;
class Rectangle{
private:
double lenth,width;
public:
Rectangle(double l,double w):lenth(l),width(w){
}
bool operator<(Rectangle other){
if((other.lenth*other.width)>(lenth*width)){
return true;
}else{
return false;
}
}
bool operator>(Rectangle other){
if((other.lenth*other.width)<(lenth*width)){
return true;
}else{
return false;
}
}
bool operator=(Rectangle other){
if((other.lenth*other.width)==(lenth*width)){
return true;
}else{
return false;
}
}
};
int main(){
Rectangle R1(10,10);
Rectangle R2(20,20);
Rectangle R3(10,10);
cout<<R1.operator<(R2)<<endl;
cout<<R2.operator>(R1)<<endl;
cout<<R1.operator=(R3)<<endl;
return 0;
}
8,9.必考题!!虚函数,纯虚函数,抽象类。基类指针指向派生类对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;
//既然是一个抽象类,那肯定有一个纯虚函数咯。
class Shape{
//纯虚函数的定义:virtual 返回类型 函数名()=0; 注意记得要写public哦!
public:
virtual void show()=0;
};
class Square:public Shape{
private:
double width,lenth;
public:
Square(double l,double w):lenth(l),width(w){
}
void show(){
cout<<"正方形面积为:"<<width*lenth;
}
};
class Trapezoid:public Shape{
private:
double on,down,higth;
public:
Trapezoid(double o,double d,double h):on(o),down(d),higth(h){
}
void show(){
cout<<"梯形面积为:"<<((on+down)*higth)/2;
}
};
class Triangle:public Shape{
private:
double down,higth;
public:
Triangle(double d,double h):down(d),higth(h){
}
void show(){
cout<<"三角形面积为:"<<(down*higth)/2;
}
};
int main(){
Shape *pa[3];
Square s(10,10);
Trapezoid td(6,7,10);
Triangle te(3,4);
pa[0]=&s; //基类指针指向派生类对象
pa[1]=&td;
pa[2]=&te;
for(int i=0;i<3;i++){
pa[i]->show();
}
return 0;
}

五:练习题

选择题型

image-20200721150321074

面向对象程序设计的三大特性:封装、继承、多态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2.关于对象和类的关系,说法正确的是( C  )。 
A) 同属于一类的对象,具有相同的数据成员和成员函数。 
B) 对象是具体,是类的对象,同其他变量一样,先定义后使用。 
C) 同一类的不同对象,其具有的操作可不同,具体的操作也不同。 
D) 不同类的对象,可有相同的操作。

3.下列符号中可以用作C++标识符的是( A  )。     
A) _radius      B) foo~bar    C) else        D)  3room

3.下列合法的标识符是(D   )。 A) abde+    B)#KSJF    C)67KDJ     D)DK3- 

4.下列关于实参和形参说法错误的是(  D )。  
A)实参可以是变量、常量、或表达式。 
B)实参与形参的类型必须一致,否则会发生“类型不匹配”的错误。 
C)实参对形参的数据传递是单向传递,调用结束后,实参单元被释放。 
D)形参必须是变量 。

image-20200721150354811

const:符号常量为了提高函数的可维护性

image-20200721150410677

重载肯定是为了使用更加方便啦

image-20200721150423462

重点哦:C++用常量修饰成员函数时const在括号后面,A

const 修饰类成员函数,其目的是防止成员函数修改被调用对象的值,如果我们不想修改一个调用对象的值,所有的成员函数都应当声明为 const 成员函数。

注意:const 关键字不能与 static 关键字同时使用,因为 static 关键字修饰静态成员函数,静态成员函数不含有 this 指针,即不能实例化,const 成员函数必须具体到某一实例。

image-20200721190110107

image-20200721150449469

必考:构造函数可以没有返回值

image-20200721185940783

image-20200721190049465

答案应该是C:

析构函数一个类只能有一个,析构函数的创建:~类名(){}

析构函数不能有参数

image-20200721190409901

拷贝构造函数的创建:类名(类名 &对象名){}

某个对象的引用名

image-20200721150533436

答案:没有

原因:

this指针是在指向类成员本身,但是友元函数并不在类里面,而是在类外面。

举个例子,有两个类A和B,函数C是这两个类的友元,假如C里面可以有this,那么this到底是类A的成员呢,还是类B的成员呢?

1
2
3
4
5
1.如果类A被说明成类B的友元,则( D  )。   
A.类A的成员即类B的成员。                     
B.类B的成员即类A的成员。  
C.类A的成员函数不得访问类B的成员。       
D.类B不一定是类A的友元。

image-20200721190242094

image-20200721190339109

必考:

友元函数的定义:不是一个类的成员函数,但可以像成员函数一样访问该类的所有成员,包括私有成员和保护成员,这种函数称为友元函数。

对于友元函数的定义:

friend 类型 <函数名> (形参)

一个类的友元函数一般将该类的引用作为函数参数。

友元函数既不对称也不传递,可以出现在private部分也可以出现在public部分,整个类都可以声明为另一个类的友元。

image-20200721190156220

1.静态成员也会考的:必须在类外定义

​ 类外定义也是必考的,如何进行类外定义?

​ 类型 类名::变量名=值;

2.静态成员函数:可以在类内定义,也可以在类外定义

image-20200721150543031

  1. const 关键字不能与 static 关键字同时使用,因为 static 关键字修饰静态成员函数,静态成员函数不含有 this 指针,即不能实例化,const 成员函数必须具体到某一实例。
  2. 只有在类的非静态成员函数中才可以使用this指针,其它任何函数都不可以
  3. 友元函数可以访问该类的所有成员,不管私有、公有的、保护的
  4. 也可以通过类名调用

image-20200721150617857

答案:B

image-20200721150626695

答案:虚基类主要为了消除二义性问题

image-20200721150643081

纯虚函数重点:定义:virtual void show()=0;

​ 1.含有纯虚函数的类是一个抽象类,一个抽象类一定有纯虚函数

​ 2.抽象类不能实例化

​ 3.抽象类的派生类如果没有实现所有的纯虚函数,那么他的派生类还是一个抽象类

运算符重载重点:

什么是运算符的重载?

​ 运算符与类结合,产生新的含义。

为什么要引入运算符重载?

​ 作用:为了实现类的多态性(多态是指一个函数名有多种含义)

不可重载的运算符列表:

  • **.**:成员访问运算符

  • .*, ->*:成员指针访问运算符

  • **::**:域运算符

  • sizeof:长度运算符

  • **?:**:条件运算符

  • **#**: 预处理符号

    答案:C:运算符重载不是创造了新的运算符,而是改造利用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1.下列关于虚函数的说明中,正确的是(  B )。     
A) 从虚基类继承的函数都是虚函数。     
B) 虚函数不得是静态成员函数。     
C) 只能通过指针和引用调用虚函数。     
D) 抽象类中的成员函数都是虚函数。

2.运算符重载是对已有的运算符赋予多重含义,因此( C  )。 
A)可以对基本类型(如int类型)的数据,重新定义“+”运算符的含义。 
B)可以改变一个已有运算符的优先级和操作数个数。 
C)只能重载C++中已经有的运算符,不能定义新运算符。 
D)C++中已经有的所有运算符都可以重载

3.运算符重载是对已有的运算符赋予多重含义,因此( C  )。 
A)可以对基本类型(如int类型)的数据,重新定义“+”运算符的含义。 
B)可以改变一个已有运算符的优先级和操作数个数。 
C)只能重载C++中已经有的运算符,不能定义新运算符。 
D)C++中已经有的所有运算符都可以重载

填空题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
3. 在C++语言中,惟一的一个三目运算运算符是(  ? :    )

3.一个类中可以有(多)个构造函数,只能有(  一  )个析构函数。

15、类的成员分为_数据成员_和_成员函数_。

20、声明虚函数的方法是在基类中的成员函数原型前加上关键字_virtual_。 

21、如果一个类中有一个或多个纯虚函数,则这个类称为_虚基类_。 

22、静态数据成员在定义或说明时,前面要加上关键字_static_。 

23、如果成员函数不需要访问类的_非静态_成员,则可以把它声明为静态成员函数。

24、友元可以是_全局函数_,也可以是_其他类的成员函数_。 

25、若需要把一个函数“void fun();”定义为一个类A的友元函数,则应在类A的定义中加入一条语句:_friend void fun();_。 

26、运算符函数中的关键字是_operator_,它和_运算符_一起组成该运算符函数的函数名。 

27、类中运算符重载一般采用两种形式:_成员函数_和_友元函数_。 

28、面向对象程序设计的3大机制为:_封装性_、_继承性_和_多态性_。

29、类的访问权限有_public_、_private_和_protected_三种。

33、类的静态成员分为_静态数据成员_和_静态成员函数_。

32、在类定义中,将_=0_置于虚函数的函数原型的末尾可以声明该函数为纯虚函数。

简答题:

image-20200721154710851

image-20200721154729121

大题题型:

1.找错题,运行结果题

运行结果题:

如下题:主要考的是构造函数的调用时间,以及派生类构造函数调用。和析构函数,拷贝构造函数的调用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;
class CGoods{
private:
long no;
char *p_name;
double price;
int count;
public:
CGoods(long n,char *pn,double pr,int c):no(n),p_name(pn),price(pr),count(c){
cout<<"CGoods构造函数被调用"<<endl;
}
CGoods(CGoods &p){
no=p.no;
p_name=p.p_name;
price=p.price;
count=p.count;
cout<<"CGoods拷贝构造函数被调用"<<endl;
}
~CGoods(){
cout<<"CGoods析构函数被调用"<<endl;
}
};
class CClothes:public CGoods{
private:
char *p_brand;
public:
CClothes(long n,char *pn,double pr,int c,char *pb):CGoods(n,pn,pr,c),p_brand(pb){
cout<<"CClothes构造函数被调用"<<endl;
}
CClothes(CClothes &c):CGoods(c){

p_brand=c.p_brand;
cout<<"CClothes拷贝构造函数被调用"<<endl;
}
~CClothes(){cout<<"CClothes析构函数被调用"<<endl;}
};

int main(){
CGoods g1(1009,"茶",100,200);
CGoods g2(g1);
CClothes(1010,"鞋子",200,200,"耐克");

}

<1>在main函数中为下面代码时,输出结果为什么?

1
2
3
4
int main(){
CGoods g1(1009,"茶",100,200);
CGoods g2(g1);
}

输出结果为

1
2
3
4
5
6
/*
CGoods构造函数被调用
CGoods拷贝构造函数被调用
CGoods析构函数被调用
CGoods析构函数被调用
*/

解析:创建g1对象时首先调用构造函数,创建g2时调用的是拷贝构造函数,然后在对象销毁前调用析构函数。

<2>在main函数中为下面代码时,输出结果为什么?

1
2
3
int main(){
CClothes c1(1010,"鞋子",200,200,"耐克");
}

输出结果为

1
2
3
4
5
6
/*
CGoods构造函数被调用
CClothes构造函数被调用
CClothes析构函数被调用
CGoods析构函数被调用
*/

解析:创建c1对象时首先调用基类的CGoods的构造函数,然后调用派生类的CClothes的构造函数,然后再对象c1销毁前首先调用派生类的析构函数,然后调用基类的析构函数。

<3>在main函数中为下面代码时,输出结果为什么?

1
2
3
4
5
int main(){
CGoods g1(1009,"茶",100,200);
CGoods g2(g1);
CClothes c1(1010,"鞋子",200,200,"耐克");
}

输出结果为

1
2
3
4
5
6
7
8
9
10
/*
CGoods构造函数被调用
CGoods拷贝构造函数被调用
CGoods构造函数被调用
CClothes构造函数被调用
CClothes析构函数被调用
CGoods析构函数被调用
CGoods析构函数被调用
CGoods析构函数被调用
*/
2.超经典列题:

构造、拷贝构造、析构、纯虚函数、抽象类、继承、友元函数、运算符重载都考到了。

image-20200721145935401

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
class CGoods{
private:
long no;
char *p_name;
double price;
public:
CGoods(long n,char *pn,double pr):no(n),p_name(pn),price(pr){
}
CGoods(CGoods &p){
no=p.no;
p_name=p.p_name;
price=p.price;
}
~CGoods(){
}
void show(){
cout<<"商品号:"<<no<<endl<<"商品名:"<<p_name<<endl<<"商品价格:"<<price<<endl;
}
};
int main(){
CGoods g1(1009,"茶",100);
g1.show();
return 0;
}

image-20200721150029740

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;
class CGoods{
private:
long no;
char *p_name;
double price;
int count;
public:
CGoods(long n,char *pn,double pr,int c):no(n),p_name(pn),price(pr),count(c){
}
CGoods(CGoods &p){
no=p.no;
p_name=p.p_name;
price=p.price;
count=p.count;
}
~CGoods(){
}
void show(){
cout<<"商品号:"<<no<<endl<<"商品名:"<<p_name<<endl<<"商品价格:"<<price<<endl;
}
void getCount(){
cout<<"商品总数:"<<count<<endl;
}
friend void getName(CGoods &p){
char *name=p.p_name;
cout<<"商品名:"<<name<<endl;
}
};
int main(){
CGoods g1(1009,"茶",100,200);
CGoods g2(1010,"水",100,200);
g1.show();
g1.getCount();
getName(g1);
getName(g2);
return 0;
}

image-20200721150046184

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace std;
class CGoods{
private:
long no;
char *p_name;
double price;
int count;
public:
CGoods(long n,char *pn,double pr,int c):no(n),p_name(pn),price(pr),count(c){
}
CGoods(CGoods &p){
no=p.no;
p_name=p.p_name;
price=p.price;
count=p.count;
}
~CGoods(){
}
void show(){
cout<<"商品号:"<<no<<endl<<"商品名:"<<p_name<<endl<<"商品价格:"<<price<<endl;
}
void getCount(){
cout<<"商品总数:"<<count<<endl;
}
friend void getName(CGoods &p){
char *name=p.p_name;
cout<<"商品名:"<<name<<endl;
}
bool operator<(CGoods &g){
if(price<g.price){
return true;
}else{
return false;
}
}
bool operator>=(CGoods &g){
if(price>=g.price){
return true;
}else{
return false;
}
}
};
int main(){
CGoods g1(1009,"茶",100,200);
CGoods g2(1010,"水",2,200);
g1.show();
g1.getCount();
getName(g1);
getName(g2);
cout<<"g1<g2吗?"<<g1.operator<(g2)<<endl;
cout<<"g1>=g2吗?"<<g1.operator>=(g2)<<endl;
return 0;
}

image-20200721150100719

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
using namespace std;
class CGoods{
private:
long no;
char *p_name;
double price;
int count;
public:
CGoods(long n,char *pn,double pr,int c):no(n),p_name(pn),price(pr),count(c){
}
CGoods(CGoods &p){
no=p.no;
p_name=p.p_name;
price=p.price;
count=p.count;
}
~CGoods(){
}
void show(){
cout<<"商品号:"<<no<<endl<<"商品名:"<<p_name<<endl<<"商品价格:"<<price<<endl;
}
void getCount(){
cout<<"商品总数:"<<count<<endl;
}
friend void getName(CGoods &p){
char *name=p.p_name;
cout<<"商品名:"<<name<<endl;
}
bool operator<(CGoods &g){
if(price<g.price){
return true;
}else{
return false;
}
}
bool operator>=(CGoods &g){
if(price>=g.price){
return true;
}else{
return false;
}
}
};
class CClothes:public CGoods{
private:
char *p_brand;
public:
CClothes(long n,char *pn,double pr,int c,char *pb):CGoods(n,pn,pr,c),p_brand(pb){
}
CClothes(CClothes &c):CGoods(c){
p_brand=c.p_brand;
}
~CClothes(){}
void userFor(){
cout<<"服装信息:"<<p_brand<<endl;
}
};
class CFood:public CGoods{
private:
char *p_brand;
public:
CFood(long n,char *pn,double pr,int c,char *pb):CGoods(n,pn,pr,c),p_brand(pb){
}
CFood(CFood &c):CGoods(c){
p_brand=c.p_brand;
}
~CFood(){}
void userFor(){
cout<<"商品信息:"<<p_brand<<endl;
}
};
int main(){
CGoods g1(1009,"茶",100,200);
CGoods g2(1010,"水",2,200);
g1.show();
g1.getCount();
getName(g1);
getName(g2);
cout<<"g1<g2吗?"<<g1.operator<(g2)<<endl;
cout<<"g1>=g2吗?"<<g1.operator>=(g2)<<endl;

CClothes c1(1011,"鞋",200,300,"耐克");
c1.show();
c1.getCount();
c1.userFor();

CFood f1(1012,"面包",10,500,"米利奇");
f1.show();
f1.getCount();
f1.userFor();
return 0;
}

普通小题:

image-20200721155510052

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;
class Shape{
public:
virtual void GetArea()=0;
};
class Rectangle:public Shape{
private:
double m_Width,m_Height;
public:
Rectangle(double w,double h):m_Width(w),m_Height(h){
}
Rectangle(Rectangle &r){
m_Width=r.m_Width;
m_Height=r.m_Height;
}
~Rectangle(){
}
void GetArea(){
cout<<"Rectangle的面积为:"<<m_Width*m_Height<<endl;
}
};
class Circle:public Shape{
private:
double m_Radius;
public:
Circle(double r):m_Radius(r){
}
Circle(Circle &c){
m_Radius=c.m_Radius;
}
~Circle(){}
void GetArea(){
cout<<"Circle的面积为:"<<(m_Radius*3.14)*(m_Radius*3.14)<<endl;
}
};
int main(){
Rectangle r1(2,3);
r1.GetArea();
Circle c1(2);
c1.GetArea();
return 0;
}
-------------------本文结束 感谢您的阅读-------------------