//2020年05月03日 16时06分53秒
//https://blog.csdn.net/zhanglei0107/article/details/12234031?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3
#include
using namespace std;
class Base{
public:
Base()
{
cout<<“Base constructor”<<endl;
}
};
//class A:public Base{ //例题1
class A : virtual public Base{
public:
A()
{
cout<<“A constructor”<<endl;
}
};
//class B: public Base{ //例题1
class B : virtual public Base{
public:
B()
{
cout<<“B constructor”<<endl;
}
};
class E:public Base{ 例题3
public:
E()
{
cout<<“E constructor”<<endl;
}
};
//class D:public A,B{ //例题2
class D:public A,B,E{ //例题3
public:
D(){
cout<<“D constructor”<<endl;
}
};
// 例题1 A的基类是Base,B的基类也是Base,而类D多继承自A,B,可以看到基类Base的构造函数被调用了两次
// 例题2 虚基类只会创建虚基类的一个对象。 (只调用一次基类的构造函数)。虚基类的构造函数由继承层次最深的类调用 (if a class inherits one or more classes that have virtual parents, the most derived class is responsible for constructing the virtual base class.)如果要识别某类的父类为虚基类,该派生类的继承列表前必须添加virtual关键字。否则将不识别该类的父类为虚基类
//https://blog.csdn.net/zhanglei0107/article/details/12234031
int main(){
D d;
}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17
// 例题1
Base constructor
A constructor
Base constructor
B constructor
D constructor
// 例题2
Base constructor
A constructor
B constructor
D constructor
//基类构造函数只调用一次,基类构造函数由类D调用。
//例题3
Base constructor
A constructor
B constructor
Base constructor
E constructor
D constructor
//基类Base构造函数调用了两次,因为派生类E继承列表前未添加virtual关键字,不表明该类E继承自虚基类,仍会调用基类构造函数
//https://www.learncpp.com/cpp-tutorial/121-pointers-and-references-to-the-base-class-of-derived-objects/
#include
#include <string_view>
#include
class Animal
{
protected:
std::string m_name;
// We’re making this constructor protected because
// we don’t want people creating Animal objects directly,
// but we still want derived classes to be able to use it.
Animal(const std::string &name)
: m_name{ name } {
}
public:
const std::string& getName() const { return m_name; }
std::string_view speak() const { return “???”; }
};
class Cat: public Animal
{
public:
Cat(const std::string &name)
: Animal{ name } {
}
std::string_view speak() const { return "Meow"; }
};
class Dog: public Animal
{
public:
Dog(const std::string &name)
: Animal{ name } {
}
std::string_view speak() const { return "Woof"; }
};
int main()
{
Cat cat{ “Fred” };
std::cout << "cat is named " << cat.getName()
<< ", and it says " << cat.speak() << ‘\n’;
Dog dog{ "Garbo" };
std::cout << "dog is named " << dog.getName()
<< ", and it says " << dog.speak() << '\n';
Animal *pAnimal{ &cat };
std::cout << "pAnimal is named " << pAnimal->getName()
<< ", and it says " << pAnimal->speak() << '\n';
pAnimal = &dog;
std::cout << "pAnimal is named " << pAnimal->getName()
<< ", and it says " << pAnimal->speak() << '\n';
return 0;
}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17
cat is named Fred, and it says Meow
dog is named Garbo, and it says Woof
pAnimal is named Fred, and it says ???
pAnimal is named Garbo, and it says ???
//professional c++ 4th edition p 187-190
#include
#include
class Base { // +final 错误:cannot derive from ‘final’ base ‘Base’ in derived type ‘Derived’
public:
virtual void someMethod()
{
std::cout << “Base constructor” << std::endl;
//std::cout << “This is Base’s version of someMethod().” << std::endl;
}
protected: //p187
//int mProtectedInt(); //错误:对非静态成员函数‘int Base::mProtectedInt()’的使用无效
static int mProtectedInt();
private:
int mPrivateInt();
};
class Derived : public Base{ // : 打错了 ::
public:
virtual void someOtherMethod(){
std::cout << “Derived constructor” << std::endl;
std::cout << “I can access base class data member mProtectedInt.” << std::endl;
std::cout << "Its value is " << mProtectedInt << std::endl;
//std::cout << "The value of mPrivateInt is " << mPrivateInt << std::endl;
}
virtual void someMethod() override{
std::cout << “This is Derived’s version of someMethod().” << std::endl;
}
};
int main(){
Derived myDerived;
myDerived.someMethod(); //c17.cpp:(.text+0x10):对‘Base::someMethod()’未定义的引用
Base& ref = myDerived; //p189
ref.someMethod(); //This is Derived's version of someMethod().
Base assignedObject = myDerived;
assignedObject.someMethod(); //Base constructor
std::cout << std::endl;
Base myBased;
myBased.someMethod();
std::cout << std::endl;
myDerived.someOtherMethod(); //c17.cpp:(.text+0x1c):对‘Derived::someOtherMethod()’未定义的引用
std::cout << std::endl;
Base* base = new Derived();
base->someMethod();
delete base;
}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17
Base constructor
Derived constructor
I can access base class data member mProtectedInt.
Its value is 1
Base constructor
This is Derived’s version of someMethod().
This is Derived’s version of someMethod().
Base constructor
Derived constructor
I can access base class data member mProtectedInt.
Its value is 1
This is Derived’s version of someMethod().