C++运算符重载的方法详细解析

C++运算符重载的方法详细解析_第1张图片



C++运算符重载的方法详细解析_第2张图片



#include

using namespace std;

class Person

{

public:

Person()

{

cout << "Person()" << endl;

}

Person(char a)

{

sexy=a;

cout << "Person(char a)" << endl;

}

void info()

{

cout << "sexy=" << sexy << endl;

}

private:

char sexy;

};

class Student:public Person

{

public:

Student()

{

cout << "Student()" << endl;

}

Student(string a,int b,char c):Person(c)

{

name =a;

age =b;

}

void info()

{

cout << "name=" << name << "\n" << "age=" << age << endl;

}

private:

string name;

int age;

};

int main()

{

Student stu("zhangsan",18,'f');

stu.info();

stu.Person::info();

//注意格式,不是Person::stu.info();否则会提示stu is not a member of ‘Person’

return 0;

}


C++运算符重载的方法详细解析_第3张图片

你可能感兴趣的:(C++运算符重载的方法详细解析)