类Man
、WoMan
继承于类Person
。
类Man
、WoMan
的实例对象都可以使用类Person
中public属性的成员及成员函数。
#include
class Person
{
public:
//friend Man;
Person() { m_age = 0; }
Person(int age):m_age(age){};
void spleep() { std::cout << "Sleeping zzz" << std::endl; }
void speak() { std::cout << "Speaking" << std::endl; }
void play() { std::cout << "Playing ..." << std::endl; };
virtual ~Person() {};
private:
int m_age;
int get_age() { return m_age; }
};
class Man: public Person
{
public:
Man() {};
void play() { std::cout << "Playing basketball!" << std::endl; }
~Man() {};
};
class WoMan : public Person
{
public:
WoMan() {};
void play() { std::cout << "Playing tennis!" << std::endl; }
~WoMan() {};
};
int main()
{
Person p1;
p1.play();
Man m1;
m1.spleep();
WoMan wm1;
wm1.speak();
return 0;
}
//Playing ...
//Sleeping zzz
//Speaking
静态成员变量不专属于某一个类对象,可以通过类或者类对象进行访问。
静态成员变量处于全局区。
静态成员函数也是相似的,可以通过类或者类对象进行访问。
静态成员函数只能访问静态变量,不能访问非静态变量。
如m_salary
非静态变量,则:
static void get_salary() { cout << "static func: " << m_salary << endl; }
会报错。
完整demo如下:
#include
using namespace std;
class Person
{
public:
static int m_age;
int m_salary=10;
static void get_age() { cout <<"static func: "<< m_age << endl; }
//static void get_salary() { cout << "static func: " << m_salary << endl; }
};
int Person::m_age = 10;
int main()
{
Person p;
cout << "static var: " << p.m_age << endl;
cout << "static var: " << Person::m_age<< endl;
p.get_age() ;
Person::get_age();
return 0;
}
//static var: 10
//static var: 10
//static func: 10
//static func: 10
类Man
、WoMan
继承于类Person
,同时这三个类中都有成员函数play
。这就是多态。
#include
class Person
{
public:
Person() { }
void play() { std::cout << "Playing ..." << std::endl; };
~Person() {};
};
class Man: public Person
{
public:
Man() {};
void play() { std::cout << "Playing basketball!" << std::endl; }
~Man() {};
};
class WoMan : public Person
{
public:
WoMan() {};
void play() { std::cout << "Playing tennis!" << std::endl; }
~WoMan() {};
};
实例化并打印结果:
int main()
{
Person p1;
p1.play();
Man m1;
m1.play();
WoMan wm1;
wm1.play();
return 0;
}
//Playing ...
//Playing basketball!
//Playing tennis!
当基类指针指向派生类对象时,调用时却使用了基类中相应的成员函数。
int main()
{
Man m1;
Person *p1 = &m1;
p1->play();
}
//Playing ...
解决方法是在父类的成员函数前,加上virtual
关键字。
另外,子类对应的成员函数定义时,也可加上 override
关键字(可选,推荐)。
#include
class Person
{
public:
Person() { }
virtual void play() { std::cout << "Playing ..." << std::endl; };
~Person() {};
};
class Man: public Person
{
public:
Man() {};
void play() override { std::cout << "Playing basketball!" << std::endl; }
~Man() {};
};
class WoMan : public Person
{
public:
WoMan() {};
void play()override { std::cout << "Playing tennis!" << std::endl; }
~WoMan() {};
};
int main()
{
Man m1;
Person *p1 = &m1;
p1->play();
}
//Playing basketball!
这样仍然可以调用子类的成员函数。
在父类中对某成员函数不进行实现,而放在子类中具体实现,就要用到纯虚函数。
virtual void play() =0;
它会强制要求子类进行实现。
#include
class Person
{
public:
Person() { }
virtual void play() =0;
~Person() {};
};
class Man: public Person
{
public:
Man() {};
void play() override { std::cout << "Playing basketball!" << std::endl; }
~Man() {};
};
class WoMan : public Person
{
public:
WoMan() {};
void play()override { std::cout << "Playing tennis!" << std::endl; }
~WoMan() {};
};
int main()
{
Man m1;
Person *p1 = &m1;
p1->play();
}
//Playing basketball!
类进行实例化后,通过其对象不能访问类的私有属性和函数。
如这个例子中,Person
类的m_age
是其私有属性。若Friend
类想要访问,则可以将在Person
定义中将其添加为友元类。
friend class Friend;
完整代码如下:
#include
class Person;
class Friend
{
public:
Friend() {};
void get_age(Person p);
~Friend() {};
};
class Person
{
public:
friend class Friend;
Person(int age) :m_age(age) {};
virtual ~Person() {};
private:
int m_age;
};
void Friend::get_age(Person p)
{
std::cout << p.m_age<<std::endl;
}
int main()
{
Person p1(10);
Friend f1;
f1.get_age(p1);
return 0;
}
//10
除了将整个类都作为友元类,还可以定义将类的函数作为其他类的友元。
如将 Friend
的get_age
成员函数作为Person
类的友元函数。
friend void Friend::get_age(Person p);
完整代码如下:
#include
class Person;
class Friend
{
public:
Friend() {};
void get_age(Person p);
~Friend() {};
};
class Person
{
public:
friend void Friend::get_age(Person p);;
Person() { m_age = 0; }
Person(int age) :m_age(age) {};
virtual ~Person() {};
private:
int m_age;
};
void Friend::get_age(Person p)
{
std::cout << p.m_age<<std::endl;
}
int main()
{
Person p1(10);
Friend f1;
f1.get_age(p1);
return 0;
}
//10