1.继承的本质
#include "pch.h"
#include <iostream>
#include <typeinfo>
#include <string>
using namespace std;
class A
{
public:
A(int data = 0) :ma(data) { cout << "A()" << endl; }
~A(){cout << "~A()" << endl; }
private:
int ma;
};
class B : virtual public A
{
public:
B(int data = 0) :A(data), mb(data) { cout << "B()" << endl; }
~B(){cout << "~B()" << endl; }
private:
int mb;
};
class C : virtual public A
{
public:
C(int data = 0) :A(data), mc(data) { cout << "C()" << endl; }
~C(){cout << "~C()" << endl; }
private:
int mc;
};
class D : public B, public C
{
public:
D(int data=0):B(data), C(data), md(data) { cout << "D()" << endl; }
~D(){cout << "~D()" << endl; }
private:
int md;
};
int main()
{
D d;
return 0;
}
#if 0
class Base
{
public:
Base(int data = 10) :ma(data) { cout << "Base()" << endl; }
~Base() { cout << "~Base()" << endl; }
private:
int ma;
};
class Derive : virtual public Base
{
public:
Derive(int data=20):Base(data), mb(data) { cout << "Derive()" << endl; }
~Derive() { cout << "~Derive()" << endl; }
private:
int mb;
};
int main()
{
cout << sizeof(Derive) << endl;
return 0;
}
class Base
{
public:
Base(int a) :ma(a)
{
cout << "Base()" << endl;
}
~Base() { cout << "~Base()" << endl; }
virtual void show() { cout << "Base::show()" << endl; }
protected:
int ma;
};
class Derive : public Base
{
public:
Derive(int data) :Base(data), mb(data)
{
cout << "Derive()" << endl;
}
~Derive() { cout << "~Derive()" << endl; }
virtual void show() { cout << "Derive::show()" << endl; }
private:
int mb;
};
int main()
{
Derive *p = (Derive*)new Base(10);
p->show();
delete p;
cout << typeid(*p).name() << endl;
return 0;
}
class Base
{
public:
Base(int a) :ma(a)
{
cout << "Base()" << endl;
clear();
}
~Base() { cout << "~Base()" << endl; }
void clear() { memset(this, 0, sizeof(*this)); }
virtual void show() { cout << "Base::show()" << endl; }
protected:
int ma;
};
class Derive : public Base
{
public:
Derive(int data) :Base(data), mb(data)
{
cout << "Derive()" << endl;
}
~Derive() { cout << "~Derive()" << endl; }
virtual void show() { cout << "Derive::show()" << endl; }
private:
int mb;
};
int main()
{
Base *p2 = new Derive(20);
p2->show();
delete p2;
return 0;
}
class Base
{
public:
Base(int data) :pa(new int(data)) { cout << "Base()" << endl; }
virtual ~Base() { cout << "~Base()" << endl; delete pa; }
private:
int *pa;
};
class Derive : public Base
{
public:
Derive(int data) :Base(data), pb(new int(data))
{
cout << "Derive()" << endl;
}
~Derive() { cout << "~Derive()" << endl; delete pb; }
private:
int *pb;
};
int main()
{
Base *p = new Derive(10);
delete p;
return 0;
}
class Base
{
public:
Base(int a) :ma(a)
{
cout << "Base()" << endl;
clear();
}
~Base() { cout << "~Base()" << endl; }
void clear() { memset(this, 0, sizeof(*this)); }
virtual void show() { cout << "Base::show()" << endl; }
protected:
int ma;
};
class Derive : public Base
{
public:
Derive(int data) :Base(data), mb(data)
{
cout << "Derive()" << endl;
}
~Derive() { cout << "~Derive()" << endl; }
virtual void show() { cout << "Derive::show()" << endl; }
private:
int mb;
};
int main()
{
Base *p2 = new Derive(0);
p2->show();
delete p2;
return 0;
}
class Animal
{
public:
Animal(string name) :_name(name) {}
virtual void bark() = 0;
protected:
string _name;
};
class Cat : public Animal
{
public:
Cat(string name):Animal(name){}
void bark() { cout << _name << " bark:喵喵!" << endl; }
};
class Dog : public Animal
{
public:
Dog(string name) :Animal(name) {}
void bark() { cout << _name << " bark:旺旺!" << endl; }
};
int main()
{
Animal *p1 = new Cat("猫");
Animal *p2 = new Dog("二哈");
int *p11 = (int*)p1;
int *p22 = (int*)p2;
int tmp = p11[0];
p11[0] = p22[0];
p22[0] = tmp;
p1->bark();
p2->bark();
cout << typeid(*p1).name() << endl;
return 0;
}
class Window
{
public:
Window(int t = 0)
{ a = t; cout << "window" << endl; }
virtual void onResize()
{ a = 10; cout << "call Window::onResize" << endl; }
int a;
};
class SpecialWindow :public Window
{
public:
virtual void onResize()
{
((Window)(*this)).onResize();
cout << "call SpecialWindow::onResize" << endl;
cout << Window::a << endl;
}
};
int main()
{
SpecialWindow sw;
sw.onResize();
return 0;
}
class Base
{
public:
Base(int a) :ma(a) { cout << "Base()" << endl; }
~Base() { cout << "~Base()" << endl; }
virtual void show() { cout << "Base::show()" << endl; }
virtual void show(int i) { cout << "Base::show(int)" << endl; }
protected:
int ma;
};
class Derive : public Base
{
public:
Derive(int data) :Base(data), mb(data) { cout << "Derive()" << endl; }
~Derive() { cout << "~Derive()" << endl; }
virtual void show() { cout << "Derive::show()" << endl; }
private:
int mb;
};
int main()
{
Derive d(20);
Base *pb = &d;
pb->show();
cout << sizeof(Base) << endl;
cout << sizeof(Derive) << endl;
cout << typeid(pb).name() << endl;
cout << typeid(*pb).name() << endl;
return 0;
}
#endif
#if 0
class Base
{
public:
Base(int a) :ma(a) { cout << "Base()" << endl; }
~Base() { cout << "~Base()" << endl; }
void show() { cout << "Base::show()" << endl; }
void show(int i) { cout << "Base::show(int)" << endl; }
protected:
int ma;
};
class Derive : public Base
{
public:
Derive(int data) :Base(data), mb(data) { cout << "Derive()" << endl; }
~Derive() { cout << "~Derive()" << endl; }
void show() {cout << "Derive::show()" << endl;}
private:
int mb;
};
int main()
{
Base b(10);
Derive d(20);
b = d;
Base *pb = &d;
pb->show(10);
return 0;
}
class Base
{
public:
Base(int a) :ma(a) { cout << "Base()" << endl; }
~Base() { cout << "~Base()" << endl; }
void show() { cout << "Base::show()" << endl; }
void show(int i) { cout << "Base::show(int)" << endl; }
protected:
int ma;
};
class Derive : public Base
{
public:
Derive(int data) :Base(data), mb(data) { cout << "Derive()" << endl; }
~Derive() { cout << "~Derive()" << endl; }
void show()
{
cout << "Derive::show()" << endl;
}
private:
int ma;
int mb;
};
int main()
{
Derive d(10);
d.show(20);
d.show();
d.Base::show();
return 0;
}
class A
{
public:
int ma;
protected:
int mb;
private:
int mc;
};
class B : private A
{
public:
int md;
protected:
int me;
private:
int mf;
};
class C : public B
{
};
int main()
{
B b;
cout << b.mb << endl;
std::cout << "Hello World!\n";
}
#endif
2. C++结构

3.c++继承

4.c++总结

5.c++常见问题
#include<iostream>
using namespace std;
int main()
{
return 0;
}