1 运算符重载
/**
* 1 定义外 外部的重载
* @param pos1
* @param pos2
* @return
*/
Position operator + (Position pos1 ,Position pos2){
int x = pos1.getX() + pos2.getX();
int y = pos1.getY() + pos2.getY();
return Position(x,y);
}
class Position{
private:
int x,y ;
public:
void setX(int x){
this->x = x ;
}
void setY(int Y){
this->y = y ;
}
int getX(){
return this->x;
}
int getY(){
return this->y;
}
public:
// 无参改造函数
Position(){};
Position(int x,int y) :x(x),y(y){};
// //2 内部定义 ,
// Position operator + (Position pos){
// int x = this->getX() + pos.getX();
// int y = this->getY() + pos.getY();
// return Position(x,y);
// }
//3 内部定义 常量
Position operator + (const Position & pos){
int x = this->getX() + pos.x;
int y = this->getY() + pos.y;
return Position(x,y);
}
//2 对象++ 运算符 重载
void operator ++() { // ++对象
this->x = this->x + 1;
this->y = this->y + 1;
}
void operator ++ (int) { // 对象++
this->x = this->x + 1;
this->y = this->y + 1;
}
//3 ostream 输出
friend void operator << (ostream & _START, Position pos) {
// 输出换行:<< endl;
_START << " " << pos.x << " ! " << pos.y << " " << endl;
}
//4 istream 输入 系统的
friend istream & operator >> (istream & _START, Position & pos) {
_START >> pos.x >> pos.y;
return _START;
}
};
void function(){
Position pos1(100,200) ;
Position pos2(300,400) ;
//正常情况下 + 无法使用 ;
// 如果想使用 则需要重载运算符
Position pos3 = pos1 + pos2 ;
cout << pos3.getX() << "," << pos3.getY() << endl ;
Position result(1, 2);
result++;
++result;
cout << result; // 单个的
Position res;
cin >> res; // >> 是我们自己重载的哦
cout << "你输入的是:" << res.getX() << endl;
cout << "你输入的是:" << res.getY() << endl;
}
//5 C++ 继承
class Person{
public:
char * name ;
int age ;
Person(char * name ,int age):name(name),age(age){
}
void show(){
}
};
/*
* 继承父类 默认 是 private 继承
* private 继承 内部可以使用父类的成员变量
* 外部不可以使用 ,
* 可以修改为 public 继承
* 外部依然可以使用 父类的成员变量
*/
class Worker : public Person{
public:
char * type ; // 工种
// 继承时 需要 继承父类的构造方法 也可以用逗号继续为自己的值赋值
Worker(char * name , int age, char * type) : Person(name,age) , type(type) {}
void sayName(){
// 无论 那种继承 ,内部都可以使用
cout << name <<endl;
}
};
void functino02(){
Worker worker("张三",23,"钳工") ;
//class Worker : Person{ 或 class Worker : private Person{
// cout << worker.name <<endl; // 当继承是 private 时 该方法无法访问
//class Worker : public Person{
cout << worker.name <<endl; // 当继承是 public 时 该方法可以访问
}
// C++ 是 多继承
class BaseClass1{
public:
int number = 0;
void show(){
}
};
class BaseClass2{
public:
int number = 0;
void show(){
}
};
class ZiClass : public BaseClass1,public BaseClass2{
public:
int number = 0;
void show(){
}
};
void functino03(){
ZiClass ziClass ;
ziClass.show();// 子类如果有 show方法 则直接调用 子类没有 show方法 则调用父类的
ziClass.BaseClass1::show(); //多个父类 冲突时 可以通过 class:: 来区分
ziClass.BaseClass2::show();
cout << ziClass.number << endl;// 子类如果有 number 则直接调用 子类没有 number 则调用父类的
cout << ziClass.BaseClass1::number << endl; //多个父类 冲突时 可以通过 class:: 来区分
cout << ziClass.BaseClass2::number << endl;
}
// 一级父类
class Object{
public:
int number;
void show() {
cout << "Object show run..." << endl;
}
};
// virtual 通过 virtual 的修饰 ,所有的 二级父类和一级父类共享 成员变量 与 方法 ,子类使用时 可以直接调用
// 二级父类1
class BaseActivity1 : virtual public Object {
};
// 二级父类2
class BaseActivity2 : virtual public Object {
};
// 子类
class Zi : public BaseActivity1, public BaseActivity2 {
};
void functino04(){
// virtual 通过 virtual 的修饰 ,所有的 二级父类和一级父类共享 成员变量 与 方法 ,
//子类使用时 可以直接调用
Zi zi ;
Object ob1 ;
BaseActivity1 base1 ;
BaseActivity2 base2 ;
zi.number = 100 ;
ob1.number = 200 ;
base1.number = 300 ;
base2.number = 400 ;
cout << zi.number << endl;
cout << ob1.number << endl;
cout << base1.number << endl;
cout << base2.number << endl;
}
E:\C\Project\C++\cmake-build-debug\C__.exe
100
200
300
400
Process finished with exit code 0