C++语言程序设计-谭浩强

阅读: 评论:0

2024年2月4日发(作者:)

C++语言程序设计-谭浩强

. .

第1章 C++的初步知识

一、面向对象的基本概念

❖ 对象/实例(object/instance)

❖ 类(class)

❖ 封装(encapsulation)

❖ 继承(inheritance)

❖ 多态(polymorphism)

❖ 重载(overload)

❖ 消息(message)

二、C++的开发运行环境

1、Visual C++

2、GCC:是一个用于Linu*系统下编程的编译器

3、WinTC

4、Dev-C++

三、最简单的C++程序

1、例题1.1

*include

using namespace std;

int main()

{

cout<<"This is a C++ program.";

return 0;

}

程序功能:输出一行字符:This is a C++ program.

程序注释:

(1)预处理命令*include

iostream输入、输出流

(2)using namespace std;

使用命名空间std。

第一行和第二行是每个C++都有的语句。

(3)C++的主函数名与C一样,都是main。

(4)C++的输出使用cout<<

cout是输出流对象,<<是插入运算符。

若要输出一个字符串,将要输出的字符串写在双引号中;

. ! .

. .

若要输出一个整数,<<1;

若要输出一个变量,<

例如:

*include

using namespace std;

int main()

{

int a=2;

cout<<"This is a C++ program."<<1<

return 0;

}

2、例题1.2

*include

using namespace std;

int main()

{

int a,b,sum;

cin>>a>>b;

sum=a+b;

cout<<"a+b="<

return 0;

}

程序功能:求a和b两个数之和。

程序注释:

(1)输入语句:cin>>a>>b;

cin:输入流对象

>>:提取运算符

C++中的输入、输出比C更简洁,无需格式控制。

输入时用空格或者回车分隔都可以。

若想输入一个整数,一个实数,一个字符怎么写?

*include

using namespace std;

int main()

{

int a;

float b;

char c;

cin>>a>>b>>c;

cout<<"a="<

cout<<"b="<

cout<<"c="<

return 0;

}

或者

. ! .

. .

*include

using namespace std;

int main()

{

int a;

float b;

char c;

cin>>a>>b>>c;

cout<<"a="<

return 0;

}

(2)//C++的注释符,若注释容较少,一行即可,那么可以使用//,若注释容较多,需要多行,那么使用/* */。

(3)endl是回车换行符,与’n’的作用一样。

3、例题1.3

*include

using namespace std;

int ma*(int *, int y)

{

int z;

if(*>y) z=*;

else z=y;

return(z);

}

int main()

{

int a,b,m;

cin>>a>>b;

m=ma*(a,b);

cout<<"ma*="<

return 0;

}

程序功能:给两个数*和y,求两数中的大者。

程序解释:

(1)与C完全一致。涉及到子函数和主函数。

4、例题1.4

*include

using namespace std;

class Student

{

private:

int num;

int score;

. ! .

. .

public:

void setdata()

{

cin>>num;

cin>>score;

}

void display()

{

cout<<"num="<

cout<<"score="<

}

};

Student stud1,stud2;

int main()

{

a();

a();

y();

y();

return 0;

}

程序功能:定义一个学生类和两个学生对象,输入并显示这两个学生的学号和成绩。

程序解释:

(1)class Student

类的定义

class是类定义的关键字。

Student是我们自定义的类名。

(2)private和public

private后定义的容(包括数据和函数)只允许类的成员函数使用,类外不能使用。

例如:

int main()

{

cout<<;

a();

a();

y();

y();

return 0;

}

错误提示:error c2248:’num’: cannot access private member declared in class

‘Student’

(3)类中包含了数据和函数

. ! .

. .

与结构体的最大区别。

(4)Student stud1,stud2;

定义两个学生对象

类是抽象的,而对象是具体的。

(5)a();

a();

y();

y();

调用学生类的成员函数,对两个学生对象赋值并显示。

(6)思考:如何定义新学生对象stud3,学号1003,分数80。

四、C++对C的扩充

1、函数的重载

(1)重载是什么意思?

(2)为什么C++要增加重载?

(3)重载分为

函数重载:在同一作用域中用同一函数名定义多个函数,这些函数的参数个数和参数类型不相同。

运算符重载:第十章介绍

(4)实例:求两个/三个数中最大的数(数值类型可以为整型、实型、长整型)。

用C实现必须定义6个子函数。

 int ma*1(int a, int b, int c);

 float ma*2(float a, float b, float c);

 long ma*3(long a, long b, long c);

 int ma*4(int a, int b);

 float ma*5(float a, float b);

 long ma*6(long a, long b);

int ma*1(int a, int b, int c)

{ if(b>a) a=b;

if(c>a) a=c;

return a;

}

int ma*4(int a, int b)

{ if(a>b) return a;

else return b;

}

而C++通过函数重载,可以使用同一个函数名,实现上述六个子函数的功能。

 int ma*(int a, int b, int c);

 float ma*(float a, float b, float c);

 long ma*(long a, long b, long c);

 int ma*(int a, int b);

 float ma*(float a, float b);

 long ma*(long a, long b);

. ! .

. .

程序代码:

*include

using namespace std;

int ma*(int a, int b, int c)

{

if(b>a) a=b;

if(c>a) a=c;

return a;

}

float ma*(float a, float b, float c)

{

if(b>a) a=b;

if(c>a) a=c;

return a;

}

int main()

{

int a,b,c;

float d,e,f;

cin>>a>>b>>c;

cout<

cout<

cin>>d>>e>>f;

cout<

//cout<

return 0;

}

注意:

语句cout<

error C2668: 'ma*' : ambiguous call to overloaded function

作业:

P16~P17

5、6、7、8、9、10.

. ! .

. .

第二讲

第8章 类和对象

一、面向过程的程序设计方法和面向对象的程序设计方法

1、程序功能

对学生基本信息(学号、、性别)进行输入输出。

2、面向过程的程序设计方法

*include

using namespace std;

struct Student

{

int num;

char name[20];

char se*;

};

Student get_information()

{

Student stud;

int i;

cout<<"num=";

cin>>;

cout<<"name=";

i=0;

cin>>[0];

while([i]!='*')

{

i++;

cin>>[i];

}

cout<<"se*=";

cin>>*;

return stud;

}

void display(Student stud)

{

int i;

cout<<"num:"<<<

cout<<"name:";

i=0;

while([i]!='*')

. ! .

. .

{

cout<<[i];

i++;

}

cout<

cout<<"se*:"<<*<

}

int main()

{

Student stud1;

stud1=get_information();

display(stud1);

return 0;

}

程序运行结果:

3、面向对象的程序设计方法

*include

using namespace std;

class Student

{

private:

int num;

char name[20];

char se*;

public:

void get_information()

{

int i;

cout<<"num=";

cin>>num;

cout<<"name=";

i=0;

cin>>name[0];

while(name[i]!='*')

{

i++;

cin>>name[i];

}

cout<<"se*=";

cin>>se*;

}

void display( )

{

int i;

. ! .

. .

cout<<"num:"<

cout<<"name:";

i=0;

while(name[i]!='*')

{

cout<

i++;

}

cout<

cout<<"se*:"<

}

};

int main()

{

Student stud1;

_information();

y();

return 0;

}

程序运行结果:

程序解释:

(1)类如何定义(包括数据和对数据的操作,数据的操作用函数来实现,它们之间的关系更加紧密。)

(2)private和public的作用

(3)类与结构体的区别

(4)定义对象的方法(类是抽象的,而对象是具体的)

(5)面向过程的程序设计中,数据与数据的操作是分离的,而面向对象的程序设计中,封装的思想用类来实现。

二、类的成员函数

1、类的成员函数与一般函数的区别

它是属于一个类的成员,出现在类体中。

它可以被指定为私有的,也可以被指定为公用的。将需要被外界调用的成员函数指定为public。无需或不能被外界调用的成员函数指定为私有的。

例如:

*include

using namespace std;

class Student

{

private:

int num;

char name[20];

char se*;

. ! .

. .

void get_name()

{

int i;

i=0;

cin>>name[0];

while(name[i]!='*')

{

i++;

cin>>name[i];

}

}

public:

void get_information()

{

cout<<"num=";

cin>>num;

cout<<"name=";

get_name();

cout<<"se*=";

cin>>se*;

}

void display( )

{

int i;

cout<<"num:"<

cout<<"name:";

i=0;

while(name[i]!='*')

{

cout<

i++;

}

cout<

cout<<"se*:"<

}

};

int main()

{

Student stud1;

_information();

y();

return 0;

}

程序运行结果:

. ! .

. .

或者:

*include

using namespace std;

class Student

{

private:

int num;

char name[20];

char se*;

void get_name()

{

int i;

i=0;

cin>>name[0];

while(name[i]!='*')

{

i++;

cin>>name[i];

}

}

void display_name()

{

int i;

i=0;

while(name[i]!='*')

{

cout<

i++;

}

}

public:

void get_information()

{

cout<<"num=";

cin>>num;

cout<<"name=";

get_name();

cout<<"se*=";

cin>>se*;

}

void display( )

{

cout<<"num:"<

cout<<"name:";

. ! .

. .

display_name();

cout<

cout<<"se*:"<

}

};

int main()

{

Student stud1;

_information();

y();

return 0;

}

程序运行结果:

思考:对于的输入、输出很麻烦,如果有已经设计好的字符串类,可以直接进行输入和输出就非常方便。

例如:

*include

*include

using namespace std;

class Student

{

private:

int num;

string name;

char se*;

public:

void get_information()

{

cout<<"num=";

cin>>num;

cout<<"name=";

cin>>name;

cout<<"se*=";

cin>>se*;

}

void display( )

{

cout<<"num:"<

cout<<"name:" <

cout<<"se*:"<

}

};

int main()

{

. ! .

. .

Student stud1;

_information();

y();

return 0;

}

程序运行结果:

注意与未使用string类的区别。

2、类外定义成员函数

*include

*include

using namespace std;

class Student

{

private:

int num;

string name;

char se*;

public:

void get_information();

void display( );

};

void Student::get_information()

{

cout<<"num=";

cin>>num;

cout<<"name=";

cin>>name;

cout<<"se*=";

cin>>se*;

}

void Student::display( )

{

cout<<"num:"<

cout<<"name:"<

cout<<"se*:"<

}

int main()

{

Student stud1;

_information();

y();

return 0;

}

程序运行结果:

. ! .

. .

3、若增加一个教师类,教师信息包括教工号、、性别,程序需要对教工信息进行输入、输出。

*include

*include

using namespace std;

class Student

{

private:

int num;

string name;

char se*;

public:

void get_information();

void display( );

};

class Teacher

{

private:

int num;

string name;

char se*;

public:

void get_information();

void display( );

};

void Student::get_information()

{

cout<<"Student_num=";

cin>>num;

cout<<"Student_name=";

cin>>name;

cout<<"Student_se*=";

cin>>se*;

}

void Student::display( )

{

cout<<"Student_num:"<

cout<<"Student_name:"<

cout<<"Student_se*:"<

}

void Teacher::get_information()

{

cout<<"Teacher_num=";

cin>>num;

. ! .

. .

cout<<"Teacher_name=";

cin>>name;

cout<<"Teacher_se*=";

cin>>se*;

}

void Teacher::display( )

{

cout<<"Teacher_num:"<

cout<<"Teacher_name:"<

cout<<"Teacher_se*:"<

}

int main()

{

Student stud1;

Teacher tech1;

_information();

cout<

_information();

cout<

y();

cout<

y();

return 0;

}

程序运行结果:

三、对象成员的引用

1、访问对象中的成员可以有3种方法:

(1)通过对象名和成员运算符访问对象中的成员

访问对象中成员的一般形式为:

对象名.成员名

例如:

=1001;

y( );

(2)通过指向对象的指针访问对象中的成员

class Time

{ public:

int hour;

int minute;

};

Time t, *p;

p=&t;

cout<hour;

. ! .

. .

(3)通过对象的引用变量访问对象中的成员

class Time

{ public:

int hour;

int minute;

};

Time t1;

Time &t2=t1;

cout<<;

第三讲

四、类和对象的简单应用举例

1、例题8.1 最简单的例子

程序功能:输入输出时间(包括时、分、秒)。

源代码:

*include

using namespace std;

class Time

{

public:

int hour;

int minute;

int sec;

};

int main()

{

Time t1;

cin>>;

cin>>;

cin>>;

cout<<<<":"<<<<":"<<<

return 0;

}

运行结果:

2、例题8.2 输入、输出多个时间。

源程序:

*include

using namespace std;

class Time

. ! .

. .

{

public:

int hour;

int minute;

int sec;

};

int main()

{

Time t1;

cin>>;

cin>>;

cin>>;

cout<<<<":"<<<<":"<<<

Time t2;

cin>>;

cin>>;

cin>>;

cout<<<<":"<<<<":"<<<

return 0;

}

程序运行结果:

上述程序中有相同的代码段,因此可以使用子函数对时间进行输入、输出。

源程序:

*include

using namespace std;

class Time

{

public:

int hour;

int minute;

int sec;

};

int main()

{

void set_time(Time &t);

void show_time(Time &t);

Time t1;

set_time(t1);

show_time(t1);

Time t2;

set_time(t2);

show_time(t2);

return 0;

}

. ! .

. .

void set_time(Time &t)

{

cin>>;

cin>>;

cin>>;

}

void show_time(Time &t)

{

cout<<<<":"<<<<":"<<<

}

运行结果:

对于上述程序的参数类型设置,需要特别注意。

若将之改为:

*include

using namespace std;

class Time

{

public:

int hour;

int minute;

int sec;

};

int main()

{

void set_time(Time t);

void show_time(Time t);

Time t1;

set_time(t1);

show_time(t1);

Time t2;

set_time(t2);

show_time(t2);

return 0;

}

void set_time(Time t)

{

cin>>;

cin>>;

cin>>;

}

void show_time(Time t)

{

cout<<<<":"<<<<":"<<<

}

. ! .

. .

运行结果为:

这是不正确的。

新知识点:有默认参数的函数定义:

源程序:

*include

using namespace std;

class Time

{

public:

int hour;

int minute;

int sec;

};

int main()

{

void set_time(Time &t, int hour=0, int minute=0, int sec=0);

void show_time(Time &t);

Time t1;

set_time(t1,12,23,34);

show_time(t1);

Time t2;

set_time(t2);

show_time(t2);

return 0;

}

void set_time(Time &t,int hour, int minute, int sec)

{

=hour;

=minute;

=sec;

}

void show_time(Time &t)

{

cout<<<<":"<<<<":"<<<

}

运行结果:

对程序做如下改动:

*include

using namespace std;

class Time

{

public:

int hour;

. ! .

. .

int minute;

int sec;

};

int main()

{

void set_time(Time &t, int hour=0, int minute=0, int sec=0);

void show_time(Time &t);

Time t1;

set_time(t1,12,23,34);

show_time(t1);

Time t2;

set_time(t2,12);

show_time(t2);

Time t3;

set_time(t3,12,23);

show_time(t3);

return 0;

}

void set_time(Time &t,int hour, int minute, int sec)

{

=hour;

=minute;

=sec;

}

void show_time(Time &t)

{

cout<<<<":"<<<<":"<<<

}

运行结果为:

体会默认参数的用法。

3、例题8.3(类的定义和使用)

源程序:

*include

using namespace std;

class Time

{

public:

void set_time();

void show_time();

private:

int hour;

int minute;

int sec;

. ! .

. .

};

int main()

{

Time t1;

_time();

_time();

Time t2;

_time();

_time();

return 0;

}

void Time::set_time()

{

cin>>hour;

cin>>minute;

cin>>sec;

}

void Time::show_time()

{

cout<

}

运行结果:

通过本例,学会面向对象的程序设计方法。

4、例题8.4 找出一个整型数组中的元素的最大值。

源程序:

*include

using namespace std;

class Array_ma*

{

public:

void set_value();

void ma*_value();

void show_value();

private:

int array[10];

int ma*;

};

void Array_ma*::set_value()

{

int i;

for(i=0;i<10;i++)

{

cin>>array[i];

. ! .

. .

}

}

void Array_ma*::ma*_value()

{

int i;

ma*=array[0];

for(i=1;i<10;i++)

{

if(array[i]>ma*) ma*=array[i];

}

}

void Array_ma*::show_value()

{

cout<<"ma*="<

}

int main()

{

Array_ma* arrma*;

arrma*.set_value();

arrma*.ma*_value();

arrma*.show_value();

return 0;

}

运行结果:

5、输入、输出点的坐标

*include

using namespace std;

class point

{

private:

float *coord, Ycoord;

public:

void Set*(float *)

{

*coord=*;

}

void SetY(float y)

{

Ycoord=y;

}

float Get*()

{

return *coord;

. ! .

. .

}

float GetY()

{

return Ycoord;

}

};

int main()

{ point p1;

*(1.1);

(2.2);

cout<<*()<

cout<<()<

return 0;

}

运行结果:

6、设计一个栈。

*include

using namespace std;

const int ma*size=6;

*define false 0

*define true 1

class stack

{

private:

float data[ma*size];

int top;

public:

int set_null_stack(void);

int empty(void);

void push(float a);

float pop(void);

void show();

};

int stack::set_null_stack(void)

{

top=-1;

cout<<"stack initialized."<

return 0;

}

int stack::empty(void)

{

if (top<0)

{

. ! .

. .

cout<<"stack is empty.";

return true;

}

else

{

cout<<"stack is not empty.";

return false;

}

}

void stack::push(float a)

{

if(top==ma*size-1)

{

cout<<"Stack overflow!"<

}

else

{

data[top+1]=a;

top++;

}

}

float stack::pop(void)

{

if(top==-1)

{

cout<<"An empty stack!"<

return 0;

}

else

{

top--;

cout<<"Stack top:";

return data[top+1];

}

}

void stack::show()

{

for(int i=0;i<=top;i++)

{

cout<

}

}

int main()

{

. ! .

. .

stack s1;

_null_stack();

for( int i=0; i<=ma*size-1; i++) (2*i);

cout<

();

cout<<()<

return 0;

}

运行结果:

完成第1章和第8章的习题。

第四讲 习题讲解

第1章 C++初步知识 习题

5、分析下面程序运行的结果。

*include

using namespace std;

int main( )

{

cout<<"This"<<"is";

cout<<"a"<<"C++";

cout<<"program."<

return 0;

}

6、分析下面程序运行的结果。

*include

using namespace std;

int main( )

{

int a,b,c;

a=10;

b=23;

c=a+b;

cout<<"a+b=";

cout<

cout<

return 0;

}

7、分析下面程序运行的结果。

*include

using namespace std;

. ! .

. .

int main( )

{

int a,b,c;

int f(int *, int y, int z);

cin>>a>>b>>c;

c=f(a,b,c);

cout<

return 0;

}

int f(int *, int y, int z)

{

int m;

if(*

else m=y;

if(z

return(m);

}

8、下列程序是否有错误。

int main();

{

int a,b;

c=a+b;

cout>>"a+b=">>a+b;

}

错误的地方:

(1) main()后有分号;

(2) 无a、b的输入语句;

(3) 无c的定义;

(4) 缺少*include

using namespace std;

(5) cout后应为<<。

9、正确程序

*include

using namespace std;

int main( )

{

int a,b,c;

int add(int, int);

cin>>a>>b;

c=add(a,b);

cout<<"a+b="<

return 0;

}

int add(int *, int y)

. ! .

. .

{

int z;

z=*+y;

return(z);

}

10、

程序作用:输入三个整数,将它们按照从小到大的顺序进行排列并输出。

①②③输出3 6 10

④的结果:

第8章 类和对象1、错误:成员函数和一般函数混用。

正确的程序:

*include

using namespace std;

class Time

{

public:

int hour;

int minute;

int sec;

};

int main()

{

void set_time(Time &t);

void show_time(Time &t);

Time t1;

set_time(t1);

show_time(t1);

Time t2;

set_time(t2);

show_time(t2);

return 0;

}

void set_time(Time &t)

{

cin>>;

cin>>;

cin>>;

}

void show_time(Time &t)

{

cout<<<<":"<<<<":"<<<

. !

. 习题

. .

}

2、按要求对1进行重写:

*include

using namespace std;

class Time

{

public:

void set_time(void)

{

cin>>hour;

cin>>minute;

cin>>sec;

}

void show_time(void)

{

cout<

}

private:

int hour;

int minute;

int sec;

};

Time t;

int main()

{

_time();

_time();

return 0;

}

3、在2的基础上进行修改:在类体声明成员函数,在类外定义成员函数。

*include

using namespace std;

class Time

{

public:

void set_time(void);

void show_time(void);

private:

int hour;

int minute;

int sec;

};

Time t;

void Time::set_time(void)

. ! .

. .

{

cin>>hour;

cin>>minute;

cin>>sec;

}

void Time::show_time(void)

{

cout<

}

int main()

{

_time();

_time();

return 0;

}

6、需要求3个长方柱的体积,编一个基于对象的程序。数据成员包括length(长)、width(宽)、height(高)。要求用成员函数实现以下功能:

(1)由键盘分别输入3个长方柱的长、宽、高;

(2)计算长方柱的体积;

(3)输出3个长方柱的体积。

程序如下:

*include

using namespace std;

class rectangular

{

public:

void get_value( )

{ cin>>length;

cin>>width;

cin>>highth;

}

int volume( )

{ return length*width*highth; }

private:

int length;

int width;

int highth;

};

int main( )

{

rectangular r1,r2,r3;

_value( );

cout<<"r1_volume="<<()<

_value( );

. ! .

. .

cout<<"r2_volume="<<()<

_value( );

cout<<"r3_volume="<<()<

return 0;

}

运行结果:

补充习题

1、 构造一个复数类,将复数的表示和复数的几种基本运算包含进去。要求包含的复数运算至少有复数的加、减、乘、除和取模。创建完这个类之后,请编一个程序来进行各项的验证。

*include

using namespace std;

class ple*

{

public:

void set_value();

void show_value();

ple* ple*_add(ple* c1,ple* c2);

ple* ple*_subtract(ple* c1,ple* c2);

double ple*_multiple();

double ple*_divide();

double ple*_mod();

private:

double real;

double imag;

};

void ple*::set_value()

{

cout<

cout<<"input ple*:";

cout<

cout<<"real=";

cin>>real;

cout<<"imag=";

cin>>imag;

}

void ple*::show_value()

{

cout<

cout<<"Value of ple*:";

cout<

cout<

. ! .

. .

if(imag>=0)

{

cout<<"+";

}

cout<

cout<<"i";

}

ple* ple*::ple*_add(ple* c1,ple* c2)

{

cout<

ple* c;

cout<<"ple*_add:";

=+;

=+;

return c;

}

ple* ple*::ple*_subtract(ple* c1,ple* c2)

{

cout<

ple* c;

cout<<"ple*_subtract:";

=;

=;

return c;

}

int main()

{

ple* c1,c2,c3,c4;

_value();

_value();

c3=*_add(c1,c2);

_value();

c4=*_subtract(c1,c2);

_value();

return 0;

}

. ! .

. .

第五讲

第9章 关于类和对象的进一步讨论

9.1 构造函数

9.1.1 对象的初始化

1、类的数据成员不能在声明类的时候初始化。

例如,下面的定义是错误的:

class Time

{

int hour=0;

int minute=0;

int sec=0;

}

原因:类是一种抽象类型。

9.1.2 构造函数的作用

1、构造函数:处理对象的初始化

2、构造函数:

(1)一种特殊的成员函数。

(2)不需要用户调用,在建立对象时自动执行。

(3)名字与类名相同。

(4)不具有任何类型,不返回任何值。

3、例题

*include

. ! .

. .

using namespace std;

class Time

{

public:

Time()

{

hour=0;

minute=0;

sec=0;

}

void set_time();

void show_time();

private:

int hour;

int minute;

int sec;

};

void Time::set_time()

{

cin>>hour;

cin>>minute;

cin>>sec;

}

void Time::show_time()

{

cout<

}

int main()

{

Time t1;

_time();

_time();

Time t2;

_time();

return 0;

}

运行结果:

类外定义构造函数:

*include

using namespace std;

class Time

{

public:

. ! .

. .

Time();

void set_time();

void show_time();

private:

int hour;

int minute;

int sec;

};

Time::Time()

{

hour=0;

minute=0;

sec=0;

}

void Time::set_time()

{

cin>>hour;

cin>>minute;

cin>>sec;

}

void Time::show_time()

{

cout<

}

int main()

{

Time t1;

_time();

_time();

Time t2;

_time();

return 0;

}

4、有关构造函数的使用说明P265

9.1.3 带参数的构造函数

1、构造函数不带参数,在函数体中对各数据成员赋初值,结果是该类的每个对象都得到同一组初值。

如果对不同的对象能给予不同的初值,怎么做?

2、带参数的构造函数

构造函数的首部:

构造函数名(类型1 形参1,类型2 形参2,…)

. ! .

. .

3、实参在哪里给出?

定义对象的时候给出。

定义对象的格式:

类名 对象名(实参1,实参2,…)

4、例如

例题9.2

*include

using namespace std;

class Bo*

{

public:

Bo*(int, int, int);

int volume();

private:

int height;

int width;

int length;

};

Bo*::Bo*(int h, int w, int len)

{

height=h;

width=w;

length=len;

}

int Bo*::volume()

{

return(height*width*length);

}

int main()

{

Bo* bo*1(12,25,30);

cout<<"The volume of bo* is "<

Bo* bo*2(15,30,21);

cout<<"The volume of bo* is "<

return 0;

}

9.1.4 用参数初始化表对数据成员初始化

*include

using namespace std;

class Bo*

{

. ! .

. .

public:

Bo*(int, int, int);

int volume();

private:

int height;

int width;

int length;

};

Bo*::Bo*(int h, int w, int len):height(h),width(w),length(len) { }

int Bo*::volume()

{

return(height*width*length);

}

int main()

{

Bo* bo*1(12,25,30);

cout<<"The volume of bo* is "<

Bo* bo*2(15,30,21);

cout<<"The volume of bo* is "<

return 0;

}

9.1.5 构造函数的重载

*include

using namespace std;

class Bo*

{

public:

Bo*();

Bo*(int h, int w, int len):height(h),width(w),length(len) { }

int volume();

private:

int height;

int width;

int length;

};

Bo*::Bo*()

{

height=10;

width=10;

length=10;

}

int Bo*::volume()

. ! .

. .

{

return(height*width*length);

}

int main()

{

Bo* bo*1;

cout<<"The volume of bo* is "<

Bo* bo*2(15,30,21);

cout<<"The volume of bo* is "<

return 0;

}

思考(设计多个重载的构造函数):

*include

using namespace std;

class Bo*

{

public:

Bo*();

Bo*(int h, int w, int len):height(h),width(w),length(len)

Bo*(int h);

Bo*(int h,int w);

int volume();

private:

int height;

int width;

int length;

};

Bo*::Bo*()

{

height=10;

width=10;

length=10;

}

Bo*::Bo*(int h)

{

height=h;

width=10;

length=10;

}

Bo*::Bo*(int h,int w)

{

height=h;

width=w;

. !

{ }

.

. .

length=10;

}

int Bo*::volume()

{

return(height*width*length);

}

int main()

{

Bo* bo*1;

cout<<"The volume of bo* is "<

Bo* bo*2(15,30,21);

cout<<"The volume of bo* is "<

Bo* bo*3(10);

cout<<"The volume of bo* is "<

Bo* bo*4(10,10);

cout<<"The volume of bo* is "<

return 0;

}

9.1.6 使用默认参数的构造函数

*include

using namespace std;

class Bo*

{

public:

Bo*(int h=10, int w=10, int len=10);

int volume();

private:

int height;

int width;

int length;

};

Bo*::Bo*(int h, int w, int len)

{

height=h;

width=w;

length=len;

}

int Bo*::volume()

{

return(height*width*length);

}

int main()

. ! .

. .

{

Bo* bo*1;

cout<<"The volume of bo* is "<

Bo* bo*2(15);

cout<<"The volume of bo* is "<

Bo* bo*3(15,30);

cout<<"The volume of bo* is "<

Bo* bo*4(15,30,20);

cout<<"The volume of bo* is "<

return 0;

}

9.2 析构函数

1、析构函数的作用

撤销对象占用的存之前完成一些清理工作。

用户希望在最后一次使用对象之后所执行的任何操作。

2、析构函数的说明

(1)一个特殊的成员函数。

(2)析构函数的名字是类名前加一个“~”号。

(3)析构函数不返回任何值,没有函数类型,也没有函数参数。

(4)一个类可以有多个构造函数,但只有一个析构函数。

(5)当对象生命周期结束时,对自动执行析构函数。

3、例如

*include

*include

using namespace std;

class Student

{

public:

Student(int n, string nam, char s)

{

num=n;

name=nam;

se*=s;

cout<<"Constructor called."<

}

~Student()

{

cout<<"Destructor called."<

}

void display()

{

cout<<"num:"<

. ! .

. .

cout<<"name:"<

cout<<"se*:"<

}

private:

int num;

string name;

char se*;

};

int main()

{

Student stud1(10010,"Wang_li",'f');

y();

Student stud2(10011,"Zhang_fun",'m');

y();

return 0;

}

9.3 调用构造函数和析构函数的顺序

1、先构造的后析构,后构造的先析构。

2、例如:

*include

*include

using namespace std;

class Student

{

public:

Student(int n, string nam, char s)

{

num=n;

name=nam;

se*=s;

cout<<"Constructor called."<

}

~Student()

{

cout<<"Destructor called."<

}

void display()

{

cout<<"num:"<

cout<<"name:"<

cout<<"se*:"<

}

. ! .

. .

private:

int num;

string name;

char se*;

};

int main()

{

Student stud1(10010,"Wang_li",'f');

y();

Student stud2(10011,"Zhang_fun",'m');

y();

return 0;

}

第六讲

9.4 对象数组

一、对象数组

数组中的每一个元素都是同类的对象。

例如:学生类Student

需要定义50个对象:Student stud[50];

二、对象数组的初始化问题

1、如果构造函数只有一个参数,在定义数组时可以直接在等号后面的花括号提供实参。

如:Student stud[3]={60,70,78};

2、如果构造函数有多个参数,在花括号中分别写出构造函数并指定实参。

例如:构造函数有3个参数,分别代表学生的学号、年龄、成绩。

Student stud[3] = { Student(1001,18,87), Student(1002,19,76), Student(1003,18,72) };

3、例题9.6 对象数组的使用方法

*include

using namespace std;

class Bo*

{

public:

Bo*(int h=10, int w=12, int len=15): height(h), width(w), length(len){}

int volume();

private:

int height;

int width;

int length;

};

int Bo*::volume()

{

. ! .

. .

return(height*width*length);

}

int main()

{

Bo* a[3]={Bo*(10,12,15),Bo*(15,18,20),Bo*(16,20,26)};

cout<<"volume of a[0] is "<

cout<<"volume of a[1] is "<

cout<<"volume of a[2] is "<

return 0;

}

上述程序的输出部分可用循环来实现:

*include

using namespace std;

class Bo*

{

public:

Bo*(int h=10, int w=12, int len=15): height(h), width(w), length(len){}

int volume();

private:

int height;

int width;

int length;

};

int Bo*::volume()

{

return(height*width*length);

}

int main()

{

Bo* a[3]={Bo*(10,12,15),Bo*(15,18,20),Bo*(16,20,26)};

for(int i=0;i<=2;i++)

{

cout<<"volume of a["<

}

return 0;

}

9.5 对象指针

9.5.1 指向对象的指针

1、对象的指针:对象空间的起始地址。

2、例如

*include

. ! .

. .

using namespace std;

class Time

{

public:

int hour;

int minute;

int sec;

void get_time();

};

void Time::get_time()

{

cout<

}

int main()

{

Time *pt;

Time t1;

=0;

=0;

=0;

pt=&t1;

cout<hour<

cout<minute<

cout<sec<

pt->get_time();

return 0;

}

9.5.2 指向对象成员的指针

1、指向对象成员的指针变量

存放对象成员地址的指针变量。

2、指向对象数据成员的指针

*include

using namespace std;

class Time

{

public:

int hour;

int minute;

int sec;

void get_time();

};

void Time::get_time()

{

. ! .

. .

cout<

}

int main()

{

Time *pt;

Time t1;

int *p1;

=0;

=0;

=0;

p1=&;

cout<<*p1<

pt=&t1;

cout<hour<

cout<minute<

cout<sec<

pt->get_time();

return 0;

}

3、指向对象成员函数的指针

(1)指向普通函数的指针变量

例如:

void fun();

void (*p) ( );

p=fun;

(*p)();

(2)指向公用成员函数的指针变量的一般形式

数据类型名 ( 类名::* 指针变量名) (参数表列);

指针变量指向一个公用成员函数的一般形式

指针变量名=&类名::成员函数名;

例如:

void (Time :: *p2) ();

p2=&Time::get_time;

4、例子9.7

*include

using namespace std;

class Time

{

public:

Time(int, int, int);

int hour;

int minute;

int sec;

void get_time();

. ! .

. .

};

Time::Time(int h, int m, int s)

{

hour=h;

minute=m;

sec=s;

}

void Time::get_time()

{

cout<

}

int main()

{

Time t1(10,13,56);

int *p1=&;

cout<<*p1<

_time();

Time *p2=&t1;

p2->get_time();

void(Time::*p3)();

p3=&Time::get_time;

(t1.*p3)();

return 0;

}

9.5.3 this指针

1、同一个类可以定义多个对象;

每个对象中的数据成员都分别占有存储空间;

不同的对象都调用同一个函数代码段。

2、例如:

Bo*类,定义了3个同类对象a,b,c。

( )执行时,引用对象a的height,width,length。

( )执行时,引用对象b的height,width,length。

问题是:系统怎样识别该使用哪一组height,width,length。

3、每个成员函数中都隐式的包含了一个特殊的指针,这个指针的名字是this。它是指向本类对象的指针,它的值是当前被调用的成员函数所在的对象的起始地址。

4、例如

return (height*width*length);

return (this->height*this->width*this->length);

. ! .

. .

9.6 共用数据的保护(自学)

9.7 对象的动态建立和释放

1、前面介绍的方法定义的对象是静态的,在程序运行过程中,对象所占的空间是不能随时释放的。

例如,在一个函数体中定义了一个对象,只有在该函数结束时,该对象才释放。

2、若希望在需要用到对象的时候才建立对象,而不需要它时就撤销它,释放存空间。应该如何实现?

new Bo*;

3、例如

*include

using namespace std;

class Bo*

{

public:

int height;

int width;

int length;

int volume();

};

int Bo*::volume()

{

return(height*width*length);

}

int main()

{

Bo* *pt;

pt=new Bo*;

pt->height=10;

pt->length=10;

pt->width=10;

cout<volume()<

return 0;

}

撤销一个对象delete

*include

using namespace std;

class Bo*

{

public:

int height;

int width;

. ! .

. .

int length;

int volume();

~Bo*()

{

cout<<"delete"<

}

};

int Bo*::volume()

{

return(height*width*length);

}

int main()

{

Bo* *pt;

pt=new Bo*;

pt->height=10;

pt->length=10;

pt->width=10;

cout<volume()<

delete pt;

cout<<"program ending!"<

return 0;

}

9.8 对象的赋值和复制

9.8.1 对象的赋值

1、如果一个类定义了两个或多个对象,这些同类的对象之间是可以互相赋值的。

这里的值指的是所有数据成员的值。

2、对象赋值的一般形式

对象名1 = 对象名2;

例如:

Student stud1,stud2;

...

stud2=stud1;

. ! .

. .

3、例题9.9

*include

using namespace std;

class Bo*

{

public:

Bo*(int=10,int=10,int=10);

int volume();

private:

int height;

int width;

int length;

};

Bo*::Bo*(int h, int w , int len)

{

height=h;

width=w;

length=len;

}

int Bo*::volume()

{

return(height*width*length);

. ! .

. .

}

int main()

{

Bo* bo*1(15,30,25),bo*2;

cout<<"The volume of bo*1 is "<

bo*2=bo*1;

cout<<"The volume of bo*2 is "<

return 0;

}

9.8.2 对象的复制

1、用一个已有的对象快速的复制出多个完全相同的对象。

例如:Bo* bo*2(bo*1);

2、一般形式是

类名 对象2(对象1);

功能:用对象1复制出对象2。

3、另一种形式

类名 对象名2=对象名1;

例如:Bo* bo*2=bo*1;

4、对象的复制和对象的赋值的区别

5、例如:

*include

using namespace std;

. ! .

C++语言程序设计-谭浩强

本文发布于:2024-02-04 06:05:16,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170699791652345.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:对象   函数   成员   数据   程序   定义   输入
留言与评论(共有 0 条评论)
   
验证码:
排行榜

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23