一、类的创建
#include<iostream>
using namespace std;
class Student {
// 公共权限
public:
// 属性(变量)
string ID;
string name;
string sex;
int age;
// 行为(函数)
void AddInfo(string c1,string c2,string c3,int c4) {
ID = c1;
name = c2;
sex = c3;
age = c4;
}
void ShowInfo() {
cout << "ID:" << ID << " 姓名:" << name << " 性别:" << sex << " 年龄:" << age << endl;
}
// 私有权限(类外不可访问)
private:
string Password;
// 保护权限(类外不可访问,儿子可访问父亲)
protected:
string Address;
};
int main() {
Student s1;
s1.AddInfo("001", "张三", "男", 21);
s1.ShowInfo();
s1.AddInfo("002", "李四", "女", 18);
s1.ShowInfo();
}
1、立方体类
class Cube {
public:
//行为
float get_cube_length() {
return cube_length;
}
float get_cube_width() {
return cube_width;
}
float get_cube_height() {
return cube_height;
}
void set_cube_length(float c1) {
cube_length = c1;
}
void set_cube_width(float c1) {
cube_width = c1;
}
void set_cube_height(float c1) {
cube_height = c1;
}
float get_cube_volume() {
return cube_height * cube_length * cube_width;
}
float get_cube_area() {
return 2 * (cube_height * cube_length + cube_height * cube_width + cube_length * cube_width);
}
bool isSame(Cube& c) {
if(cube_length == c.get_cube_length() && cube_height == c.get_cube_height() && cube_width == c.get_cube_width())
{
return true;
}
return false;
}
private:
//属性
float cube_length;
float cube_width;
float cube_height;
};
int main() {
Cube c1;
c1.set_cube_height(10);
c1.set_cube_width(20);
c1.set_cube_length(10);
Cube c2;
c2.set_cube_height(10);
c2.set_cube_width(20);
c2.set_cube_length(10);
if (c1.isSame(c2)) {
cout << "两个长方体一样!" << endl;
}
else {
cout << "两个长方体不一样!" << endl;
}
}
2、圆与点类
//点类
class Point {
private:
int m_x;
int m_y;
public:
void setX(int x) {
m_x = x;
}
void setY(int y) {
m_y = y;
}
int getX(){
return m_x;
}
int getY() {
return m_y;
}
};
//圆类
class Circle {
private:
int m_r;
Point m_p;
public:
void setR(int r) {
m_r = r;
}
int getR() {
return m_r;
}
void setPoint(int x, int y) {
m_p.setX(x);
m_p.setY(y);
}
Point getPoint() {
return m_p;
}
};
//判断圆与点的位置关系
void IsInCircle(Circle &c,Point &p) {
double distance;
distance = (c.getPoint().getX() - p.getX()) * (c.getPoint().getX() - p.getX()) +
(c.getPoint().getY() - p.getY()) * (c.getPoint().getY() - p.getY());
if (distance > c.getR()* c.getR()) {
cout << "点在圆外" << endl;
}
else if(distance == c.getR()* c.getR())
{
cout << "点在圆的边上" << endl;
}
else {
cout << "点在圆内" << endl;
}
}
int main() {
Circle a;
a.setPoint(10, 0);
a.setR(10);
Point p;
p.setX(10);
p.setY(10);
IsInCircle(a, p);
}
二、类的初始化与清理
class Test {
public:
// 1.初始化(类实例化就执行,只执行一次)
Test() { // 类名()
cout << "初始化..." << endl;
}
// 2.数据清除(类释放,即函数运行结束就执行,只执行一次)
~Test() { // ~类名()
cout << "数据清除..." << endl;
}
void go() {
cout << "工作中..." << endl;
}
};
int main() {
Test t1;
t1.go();
// 显示法
Test t2 = Test();
t2.go();
}
三、类的静态变量与静态函数
1、作用:对于所有对象为全局变量,即共同作用于一个变量或函数
class Test {
public:
static int st; // 类内静态变量定义
static void func() {
cout << st << endl; // 静态函数只能使用静态变量
}
};
int Test::st = 100; // 类外静态变量初始化
int main() {
Test t1;
Test t2;
t1.st = 200;
t2.func();
}
四、this的使用
class Test {
public:
int count=0;
int age;
Test& add(int age) { // 加&表示返回旧对象
this->age = age; // 1、this解决同名问题
count += age;
return *this; // 2、this返回对象本身
}
};
int main() {
Test t1;
t1.add(10).add(10);
cout << t1.count << endl;
}
五、访问类的私有内容
class Test {
friend void showage(); // 添加友好函数,允许访问私域
// friend class Vist; //添加友好类,使得该类内new Test后可访问Test私有域
//friend void Vist::print();// 添加Vist中友好函数,仅允许Vist类内的print函数访问Test私域
private:
int age = 18;
};
void showage() {
Test t1;
cout << t1.age << endl;
}
int main() {
showage();
}
六、类的继承
class 子类名:public 父类名 {
…
};
#include<iostream>
#include<string>
using namespace std;
class Man {
public:
string name;
string sex;
int age;
};
class Worker:public Man {
public:
string word_id;
Worker(string name, string sex, int age);
void dowork();
};
Worker::Worker(string name,string sex,int age) {
this->name = name;
this->sex = sex;
this->age = age;
}
int main() {
Worker worker("张三","男",18);
cout << worker.age << endl;
}