C++ 类&对象
最简单的一个例子
#include <iostream>
using namespace std;
//C++ 类定义
class Box
{
public:
double length; // 盒子的长度
double breadth; // 盒子的宽度
double height; // 盒子的高度
};
void main() {
//定义 C++ 对象
Box Box1; // 声明 Box1,类型为 Box
Box Box2; // 声明 Box2,类型为 Box
//访问数据成员
double volume = 0.0; // 用于存储体积
// box 1 详述
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 详述
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// box 1 的体积
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Box1 的体积:" << volume << endl;
// box 2 的体积
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Box2 的体积:" << volume << endl;
getchar();
}
构造函数析构函数
类的构造函数是类的一种特殊的成员函数,它会在每次创建类的新对象时执行。
构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void。构造函数可用于为某些成员变量设置初始值。
类的无参构造函数
关键代码
//构造函数声明
class Line
{
public:
void setLength(double len);
double getLength(void);
Line(); // 这是构造函数
private:
double length;
};
//构造函数实现
Line::Line(void)
{
cout << "Object is being created" << endl;
}
//创建对象
Line line;
声明Line();
实现时没有返回类型,参数为void,定义为,Line line;
全部代码
#include <iostream>
using namespace std;
class Line
{
public:
void setLength(double len);
double getLength(void);
Line(); // 这是构造函数
private:
double length;
};
// 成员函数定义,包括构造函数
Line::Line(void)
{
cout << "Object is being created" << endl;
}
void Line::setLength(double len)
{
length = len;
}
double Line::getLength(void)
{
return length;
}
// 程序的主函数
int main()
{
Line line;
// 设置长度
line.setLength(6.0);
cout << "Length of line : " << line.getLength() << endl;
getchar();
return 0;
}
类的带参构造函数
关键代码
//构造函数声明,带了参的
class Line
{
public:
void setLength(double len);
double getLength(void);
Line(double len); // 这是构造函数
private:
double length;
};
//构造函数实现,包括构造函数
Line::Line(double len)
{
cout << "Object is being created, length = " << len << endl;
length = len;
}
//创建对象
Line line(10.0);
声明Line(double len);
实现时没有返回类型,参数为double len
,创建对象为,Line line(10.0);
全部代码
#include <iostream>
using namespace std;
class Line
{
public:
void setLength(double len);
double getLength(void);
Line(double len); // 这是构造函数
private:
double length;
};
// 成员函数定义,包括构造函数
Line::Line(double len)
{
cout << "Object is being created, length = " << len << endl;
length = len;
}
void Line::setLength(double len)
{
length = len;
}
double Line::getLength(void)
{
return length;
}
// 程序的主函数
int main()
{
Line line(10.0);
// 获取默认设置的长度
cout << "Length of line : " << line.getLength() << endl;
// 再次设置长度
line.setLength(6.0);
cout << "Length of line : " << line.getLength() << endl;
getchar();
return 0;
}
类的析构函数
1.类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行。
2.析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀
3.它不会返回任何值,也不能带有任何参数。
4.析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。
代码
关键代码
//析构函数声明
class Line
{
public:
void setLength(double len);
double getLength(void);
Line();
~Line(); // 这是析构函数声明
private:
double length;
};
//析构函数实现
Line::~Line(void)
{
cout << "Object is being destroyed" << endl;
}
全部代码
#include <iostream>
using namespace std;
class Line
{
public:
void setLength(double len);
double getLength(void);
Line(); // 这是构造函数声明
~Line(); // 这是析构函数声明
private:
double length;
};
// 成员函数定义,包括构造函数
Line::Line(void)
{
cout << "Object is being created" << endl;
}
Line::~Line(void)
{
cout << "Object is being destroyed" << endl;
}
void Line::setLength(double len)
{
length = len;
}
double Line::getLength(void)
{
return length;
}
// 程序的主函数
int main()
{
Line line;
// 设置长度
line.setLength(6.0);
cout << "Length of line : " << line.getLength() << endl;
return 0;
}
类继承
- 下边的程序我定义了俩个类,Father和Son。
- class Son : Father,定义Son类的时候继承父类。
- 在SonMethod里边可以直接使用父类声明的方法fatherMethod()。
- 主函数内:son.SonMethod();
#include<iostream>
using namespace std;
//定义父类
class Father {
protected:
void fatherMethod();
};
void Father::fatherMethod() {
cout << "father's method" << endl;
}
//子类 继承父类
class Son : Father {
public:
void SonMethod();
};
void Son::SonMethod() {
fatherMethod();
cout << "Son's method" << endl;
}
void main() {
Son son;
son.SonMethod();
getchar();
}
输出:
father's method
Son's method\
类成员函数
- 成员函数声明 //这个很重要,不声明便没法定义成员函数,另外它声明的地方在类里边(Box)
- 成员函数定义
格式:返回值 类名::方法名(参数){ …… }
#include <iostream>
using namespace std;
class Box
{
public:
double length; // 长度
double breadth; // 宽度
double height; // 高度
// 成员函数声明
double getVolume(void);
void setLength(double len);
void setBreadth(double bre);
void setHeight(double hei);
};
// 成员函数定义
double Box::getVolume(void)
{
return length * breadth * height;
}
void Box::setLength(double len)
{
length = len;
}
void Box::setBreadth(double bre)
{
breadth = bre;
}
void Box::setHeight(double hei)
{
height = hei;
}
// 程序的主函数
int main()
{
Box Box1; // 声明 Box1,类型为 Box
Box Box2; // 声明 Box2,类型为 Box
double volume = 0.0; // 用于存储体积
// box 1 详述
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 详述
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// box 1 的体积
volume = Box1.getVolume();
cout << "Box1 的体积:" << volume << endl;
// box 2 的体积
volume = Box2.getVolume();
cout << "Box2 的体积:" << volume << endl;
getchar();
return 0;
}
重载
重载函数
- 本例程序对printf程序进行了重载
- 方法名儿相同,参数不同
#include <iostream>
using namespace std;
class printData
{
public:
void print(int i) {
cout << "整数为: " << i << endl;
}
void print(double f) {
cout << "浮点数为: " << f << endl;
}
void print(string c) {
//cout << "字符串为: " << c << endl;
}
};
int main(void)
{
printData pd;
// 输出整数
pd.print(5);
// 输出浮点数
pd.print(500.263);
// 输出字符串
pd.print("Hello C++");
getchar();
return 0;
}
输出:
整数为: 5
浮点数为: 500.263
重载运算符
主要看这俩方法:重载了+号,第一个方法是相加得出新属性相加的新对象,而后一个则纯粹是用来测试重载特性的。
// 重载 + 运算符,用于把两个 Box 对象相加
// 传入1个Box引用
Box operator + (const Box& b){
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
void operator + (string bb) {
cout << "哈哈++++呵呵" << endl;
}
对应的:函数内调用触发这俩方法
Box3 = Box1 + Box2;
.........
Box1 + "111";//注意这里是个字符串
输出:
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
哈哈++++呵呵
贴上全部代码:
//重载运算符
#include <iostream>
using namespace std;
class Box{
public:
double getVolume(void)
{
return length * breadth * height;
}
void setLength(double len)
{
length = len;
}
void setBreadth(double bre)
{
breadth = bre;
}
void setHeight(double hei)
{
height = hei;
}
// 重载 + 运算符,用于把两个 Box 对象相加
// 传入1个Box引用
Box operator + (const Box& b){
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
void operator + (string bb) {
cout << "哈哈++++呵呵" << endl;
}
private:
double length; // 长度
double breadth; // 宽度
double height; // 高度
};
// 程序的主函数
int main(){
Box Box1; // 声明 Box1,类型为 Box
Box Box2; // 声明 Box2,类型为 Box
Box Box3; // 声明 Box3,类型为 Box
double volume = 0.0; // 把体积存储在该变量中
// Box1 详述
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// Box2 详述
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// Box1 的体积
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume << endl;
// Box2 的体积
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume << endl;
// 把两个对象相加,得到 Box3
Box3 = Box1 + Box2;
// Box3 的体积
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume << endl;
Box1 + "111";
getchar();
return 0;
}