运算符重载
#include <iostream>
using namespace std;
class Kevin{
private:
int x;
int y;
public:
Kevin(int x, int y) : x(x), y(y) {};
int getX() const {
return x;
}
void setX(int x) {
Kevin::x = x;
}
int getY() const {
return y;
}
void setY(int y) {
Kevin::y = y;
}
//+ 自定义重载
//使用const 表示只读,入参不可修改
//使用& 表示传入引用,节约空间,优化性能
Kevin operator + (const Kevin & kevin) {
int x = this->x + kevin.x;
int y = this->y + kevin.y;
return Kevin(x, y);
}
//- 自定义重载
Kevin operator - (const Kevin & kevin) {
return Kevin(this->x - kevin.x, this->y - kevin.y);
}
//前++ 自定义重载
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;
}
//输出<< 自定义重载(单个)
// friend void operator << (ostream & _START, const Kevin & kevin) {
// _START << kevin.x << ", " << kevin.y << endl;
// }
//输出<< 自定义重载(多个)
friend ostream & operator << (ostream & _START, const Kevin & kevin) {
_START << kevin.x << ", " << kevin.y << endl;
return _START;
}
//输入>> 自定义重载(单个)
// friend void operator >> (istream & _START, Kevin & kevin) {
// _START >> kevin.x;
// _START >> kevin.y;
// }
//输入>> 自定义重载(多个)
friend istream & operator >> (istream & _START, Kevin & kevin) {
_START >> kevin.x >> kevin.y;
return _START;
}
//[] 自定义重载
int operator [] (int i) {
if (i == 0) {
return this->x;
} else {
return this->y;
}
}
};
int main() {
Kevin k1 = Kevin(10, 20);
Kevin k2 = Kevin(30, 10);
Kevin k3 = k1 + k2;
cout << k3.getX() << endl << k3.getY() << endl;
Kevin k4 = k1 - k2;
cout << k4.getX() << endl << k4.getY() << endl;
k1 ++;
cout << k1.getX() << endl << k1.getY() << endl;
++ k2;
cout << k2.getX() << endl << k2.getY() << endl;
//自定义输出
cout << k1 << k2;
//自定义输入
cin >> k1 >> k2;
cout << k1 << k2;
//自定义[]
cout << k1[0] << endl;
cout << k1[1] << endl;
return 0;
}
继承
#include <iostream>
using namespace std;
class Person {
private:
char * name;
public:
int age;
Person(char *name, int age) : name(name), age(age) {}
char *getName() const {
return name;
}
void setName(char *name) {
Person::name = name;
}
int getAge() const {
return age;
}
void setAge(int age) {
Person::age = age;
}
};
//默认是私有继承( : private Person)
//私有继承:在子类里面是可以访问父类的成员,但是在类的外面不行
//公开继承:可以访问父类的成员
class Student : public Person {
private:
int studyId;
public:
//子类构造中需要包含父类构造初始化
Student(int id, char * name, int age) : Person(name, age), studyId(studyId) {}
int getStudyId() const {
return studyId;
}
void setStudyId(int studyId) {
Student::studyId = studyId;
}
};
class Worker : public Person {
private:
int woreId;
public:
Worker(int id, char * name, int age) : Person(name, age), woreId(woreId) {}
int getWoreId() const {
return woreId;
}
void setWoreId(int woreId) {
Worker::woreId = woreId;
}
};
class Kevin : Student, public Worker {
public:
Kevin(int studyId, char * studyName, int studyNameAge, int workId, char * workName, int workAge)
: Student(studyId, studyName, studyNameAge),
Worker(workId, workName, workAge) {}
int getAge() const {
return Student::age;
}
};
int main() {
Kevin kevin(0, "kevin", 22, 0, "kevin", 31);
//指定父类::执行方法解决二义性
cout << kevin.Worker::getAge() << endl;
//重载父类方法解决二义性
cout << kevin.getAge() << endl;
return 0;
}
继承中构造函数和析构函数的执行顺序
class P {
public:
P() {
cout << "p start" << endl;
}
~P() {
cout << "p end" << endl;
}
};
class C : P {
public:
C() {
cout << "C start" << endl;
}
~C() {
cout << "C end" << endl;
}
};
int main() {
C c;
return 0;
//父类先构造,子类先析构
//p start
//C start
//C end
//p end
}
虚继承解决二义性
class A{
private:
int a;
public:
int getA() const {
return a;
}
void setA(int a) {
A::a = a;
}
};
class B : virtual public A{
public:
};
class C : virtual public A{
public:
};
class D : public B, public C{
public:
};
int main() {
D d;
d.setA(10);
return 0;
}
对象类型成员的赋值
#include <iostream>
using namespace std;
class X {
public:
//string是在命名空间std中对char*的封装
string name;
X(string name) : name(name) {}
};
class Y {
private:
X x;
public:
// //对象成员第一种方式,编译不能通过
// Y(const X x) {
// this->x = x;
// }
//对象成员第二种方式 对象赋值对象
Y(const X x) : x(x) {}
//对象成员第三种,对象构造方法赋值
Y(string & str) : x(str) {}
const string &getName() const {
return this->x.name;
}
};
int main() {
X x1("name1");
Y y1(x1);
cout << y1.getName() << endl;
string str = "name2";
Y y2(str);
cout << y2.getName() << endl;
return 0;
}
多态
//静态多态(重载):编译期已经决定调用哪个函数
void add(int a, int b){
cout << a + b << endl;
}
void add(int a, int b, int c){
cout << a + b + c << endl;
}
int main() {
add(10, 20);
add(10, 20, 30);
return 0;
}
//动态多态(重写):程序在运行期间才能确定调用哪个类的函数
//java默认开启,C++默认关闭
class A {
public:
//父类未使用虚函数,则关闭多态
void show1() {
cout << "A show1" << endl;
}
//父类使用虚函数,则开启多态
virtual void show2() {
cout << "A show2" << endl;
}
};
class B : public A{
public:
void show1() {
cout << "B show1" << endl;
}
void show2() {
cout << "B show2" << endl;
}
};
void show1(A * a) {
a->show1();
}
void show2(A * a) {
a->show2();
}
int main() {
B b;
show1(&b);
show2(&b);
return 0;
}
纯虚函数
class Base {
public:
//纯虚函数,相当于java的抽象函数
virtual void init() = 0;
//虚函数,子类不重写不会有问题
virtual void show() {
cout << "show" << endl;
};
};
class Main : public Base {
public:
//子类必须实现父类的纯虚函数,否则子类实例化会报错
void init() {
cout << "init" << endl;
}
};
//一个类中全是纯虚函数时,这个类就类似于java的接口
class ICallback {
public:
virtual void callback(int num) = 0;
};
//定义实现类
class MyCallbackImpl : public ICallback {
public:
void callback(int num) {
cout << num << endl;
}
};
//调用回调
void doAction(ICallback & callback) {
int num;
cin >> num;
callback.callback(num);
}
int main() {
Main main;
main.init();
main.show();
MyCallbackImpl myCallback;
doAction(myCallback);
return 0;
}
模板函数
//模板函数使用,类似java的泛型
template <typename T>
void add(T a, T b) {
cout << a + b << endl;
}
int main() {
add(10, 10);
add(10.12f, 10.13f);
return 0;
}