C++使用类和对象(谭浩强9.1-9.7)

例9.1 在例8.3的基础上,用构造函数为对象的数据成员赋初值

#include <iostream>
using namespace std;
class Time//声明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()//在类外定义show_time()成员函数并赋值
{
	cin >> hour;
	cin >> minute;
	cin >> sec;
}
void Time::show_time()//在类外定义show_time()成员函数
{
	cout << hour << ":" << minute << ":" << sec << ":" << endl;
}
int main()
{
	Time t1;//建立对象t1,同时调用构造函数t1.Time()
	t1.set_time();
	t1.show_time();
	Time t2;//建立对象t2,同时调用构造函数t2.Time()
	t2.show_time();
	return 0;
}

程序执行结果如图:
在这里插入图片描述
例9.2 带参数的构造函数,并在类外定义构造函数

#include <iostream>
using namespace std;
class Box
{
public:
	Box(int, int, int);//声明带参数的构造函数
	int volumn();
private:
	int height;
	int width;
	int length;
};
Box::Box(int h, int w, int l)//在类外定义带参数的构造函数
{
	height = h;
	width = w;
	length = l;
}
int Box::volumn()//定义成员函数
{
	return(height*width*length);
}
int main()
{
	Box box1(12, 25, 30);//建立对象box1,并指定box1的高宽长的值
	cout << "The volumn of box1 is" << box1.volumn() << endl;
	Box box2(15, 30, 21);//建立对象box2,并指定box2的高宽长的值
	cout << "The volumn of box2 is" << box2.volumn() << endl;
}

程序执行结果如图:
在这里插入图片描述
例9.3定义多个构造函数的构造函数重载用法:

#include <iostream>
using namespace std;
class Box
{
public:
	Box();//声明无参数的构造函数
	Box(int h, int w, int l) :height(h), width(w), length(l){}
	//定义一个有参的构造函数,用参数的初始化表对数据成员初始化
	int volumn();
private:
	int height;
	int width;
	int length;
};
Box::Box()//在类外定义无参数的构造函数
{
	height = 12;
	width = 25;
	length = 30;
}
int Box::volumn()//定义成员函数
{
	return(height*width*length);
}
int main()
{
	Box box1;//建立对象box1,不指定实参
	cout << "The volumn of box1 is" << box1.volumn() << endl;
	Box box2(15, 30, 21);//建立对象box2,并指定box2的高宽长的值
	cout << "The volumn of box2 is" << box2.volumn() << endl;
}

程序执行效果如图例9.2
例9.4 包含默认参数的构造函数:

#include <iostream>
using namespace std;
class Box
{
public:
	Box(int h=10, int w=10, int l=10);//声明构造函数并指定默认参数
	int volumn();
private:
	int height;
	int width;
	int length;
};
Box::Box(int h, int w, int l)//在类外定义无参数的构造函数
{
	height = h;
	width = w;
	length = l;
}
int Box::volumn()//定义成员函数
{
	return(height*width*length);
}
int main()
{
	Box box1;//建立对象box1,使用默认实参
	cout << "The volumn of box1 is" << box1.volumn() << endl;
	Box box2(15);//建立对象box2,并指定box2的一个实参
	cout << "The volumn of box2 is" << box2.volumn() << endl;
	Box box3(15,30);//建立对象box3,并指定box3的两个实参
	cout << "The volumn of box3 is" << box3.volumn() << endl;
	Box box4(15,30,20);//建立对象box4,并指定box4的三个实参
	cout << "The volumn of box4 is" << box4.volumn() << endl;
}

程序执行结果如图:
在这里插入图片描述
例9.5 包含构造函数和析构函数的C++程序

#include <iostream>
using namespace std;
#include<string>
class Student
{
public:
	Student(int n, string nam, string s) :num(n), name(nam), sex(s)
	{cout << "Constructor called." << endl;}
	//定义一个有参的构造函数,用参数的初始化表对数据成员初始化
	~Student()//定义析构函数
	{cout << "Destructor called." << endl;}
	void display()//定义成员函数
	{
		cout << "num:" << num << endl;
		cout << "name:" << name << endl;
		cout << "sex:" << sex << endl<<endl;
	}
private:
	int num;
	string name;
	string sex;
};
int main()
{
	Student stud1(10010, "Wang_Li", "f");
	//建立对象stud1并调用构造函数,并给出实参
	stud1.display();//调用成员函数display()
	Student stud2(10011, "zhang_fang", "m");
	//建立对象stud1并调用构造函数,并给出实参
	stud2.display();
	return 0;
}

执行结果如下图:
在这里插入图片描述
例9.6 用数组对象的方法,输出三个立方体的体积

#include <iostream>
using namespace std;
class Box
{
public:
	Box(int h=1, int w=12, int l=15) :height(h), width(w), length(l){}
	//定义一个有参的构造函数,用参数的初始化表对数据成员初始化
	int volumn();
private:
	int height;
	int width;
	int length;
};
int Box::volumn()//定义成员函数
{
	return(height*width*length);
}
int main()
{
	Box a[3] = {
		Box(10, 12, 15), Box(15, 18, 20), Box(16,20,26)
	};//定义对象数组,调用构造函数Box,分别提供第一、二、三个元素的实参
	cout << "The volumn of a[0] is" << a[0].volumn() << endl;
	cout << "The volumn of a[1] is" << a[1].volumn() << endl;
	cout << "The volumn of a[2] is" << a[2].volumn() << endl;
}

程序执行效果如图:
在这里插入图片描述
例9.7指向对象成员函数的指针:

#include <iostream>
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 << hour << ":" << minute << ":" << sec << endl;
}
int main()
{
	Time t1(10, 13, 56);//定义Time类对象t1并初始化
	int *p1 = &t1.hour;//定义指向整型数据的指针变量,并使其指向t1.hour
	cout << *p1 << endl;//输出片所指的数据成员t1.hour
	t1.get_time();//调用对象t1的成员函数
	Time *p2 = &t1;//定义指向Time类对象的指针p2
	p2->get_time(); //调用p2所指向对象的get_time()函数
	void(Time::*p3)();//定义指向Time类公用成员函数的指针变量p3
	p3 = &Time::get_time;//使p3指向Time类公用成员函数get_time()
	(t1.*p3)();//调用对象t1中p3所指的成员函数(即t1.get_time())
}

程序执行结果如图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值