类定义是以关键字 class 开头,后跟类的名称。类的主体是包含在一对花括号中。类定义后必须跟着一个分号或一个声明列表。
举例1:以class定义Box数据类型:
class Box
{
public:
double length; // 盒子的长度
double breadth; // 盒子的宽度
double height; // 盒子的高度
};
注:关键字 public 确定了类成员的访问属性。在类对象作用域内,公共成员在类的外部是可访问的。
举例:
#include <iostream>
using namespace std;
class Box
{
public:
double length; // 长度
double breadth; // 宽度
double height; // 高度
};
int main( )
{
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;
return 0;
}
运行结果:
Box1 的体积:210
Box2 的体积:1560
一:类成员函数
类的成员函数是指那些把定义和原型写在类定义内部的函数,就像类定义中的其他变量一样。类成员函数是类的一个成员,它可以操作类的任意对象,可以访问对象中的所有成员。
例:(内部定义)
class Box
{
public:
double length; // 长度
double breadth; // 宽度
double height; // 高度
double getVolume(void)
{
return length * breadth * height;
}
};
外部定义:
double Box::getVolume(void)
{
return length * breadth * height;
}
Box myBox; // 创建一个对象
myBox.getVolume(); // 调用该对象的成员函数
举例:
#include <iostream>
using namespace std;
//#define out_fan
#define in_fan
class Box
{
public:
double length; // 长度
double breadth; // 宽度
double height; // 高度
// 成员函数声明
#ifdef out_fan
double getVolume(void);
#endif
#ifdef in_fan
double getVolume(void)
{
return length * breadth * height;
}
#endif
void setLength( double len );
void setBreadth( double bre );
void setHeight( double hei );
};
// 成员函数定义
#ifdef out_fan
double Box::getVolume(void)
{
return length * breadth * height;
}
#endif
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;
return 0;
}
运行结果:
Box1 的体积:210
Box2 的体积:1560
注意:
1、:: 叫作用域区分符,指明一个函数属于哪个类或一个数据属于哪个类。
2、定义在类中的成员函数缺省都是内联的,如果在类定义时就在类内给出函数定义,那当然最好。如果在类中未给出成员函数定义,而又想内联该函数的话,那在类外要加上 inline,否则就认为不是内联的。
https://www.runoob.com/w3cnote/cpp-inline-usage.html
例:
// 头文件
class A
{
public:
void Foo(int x, int y);
}
// 定义文件
inline void A::Foo(int x, int y){}
3、函数调用非虚成员函数、调用虚函数的区别:
1.调用非虚成员函数:和调用非成员函数一样,通过对象确定对象所属的类,然后找到类的成员函数。此过程不会涉及到对象的内容,只会涉及对象的类型,是一种静态绑定。
2.调用虚函数与调用非虚成员函数不同,需同过虚函数表找到虚函数的地址,而虚函数表存放在每个对象中,不能再编译期间实现。只能在运行时绑定,是一种动态绑定。
例:
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdio>
#include <cstring>
using namespace std;
class student
{
public:
char name[20];
char sex[10];
float math;
float english;
float cprogram;
void input_name();
void input_sex();
void input_math();
void input_english();
void input_cprogram();
void input(class student *stu);
void show_student_massage(class student *stu);
};
void student::input_name()
{
cout << "输入学生姓名: " << endl;
cin.getline(name,sizeof(name));
cout << "学生姓名 : "<< name << endl;
}
void student::input_sex()
{
cout << "输入学生性别: " << endl;
cin.getline(sex,sizeof(sex));
}
void student::input_math()
{
cout << "输入学生数学: " << endl;
cin >> math;
}
void student::input_english()
{
cout << "输入学生英语: " << endl;
cin >> english;
}
void student::input_cprogram()
{
cout << "输入学生C语言: " << endl;
cin >> cprogram;
}
void student::show_student_massage(class student *stu)
{
cout << "学生姓名 : "<< stu->name << endl;
cout << "学生性别 : "<< stu->sex << endl;
cout << "学生数学 : "<< stu->math << endl;
cout << "学生英语 : "<< stu->english << endl;
cout << "学生C语言: "<< stu->cprogram << endl;
}
void student::input(class student *stu)
{
stu->input_name();
stu->input_sex();
stu->input_math();
stu->input_english();
stu->input_cprogram();
}
int main()
{
student xiaoming;
student *xiaoming_point = &xiaoming;
xiaoming.input(xiaoming_point);
xiaoming.show_student_massage(xiaoming_point);
return 0;
}
运行结果:
输入学生姓名: ***
学生姓名 :***
输入学生性别: **
输入学生数学: **
输入学生英语: **
输入学生C语言: **
学生姓名 : **
学生性别 : **
学生数学 : **
学生英语 : **
学生C语言:**