Day1:一句话说透: 权限总结:
- 父亲的隐私儿子不能知道
<cout << wife << end; //错!> - 他让你知道什么你就知道什么
<cout << getwife()<<endl; >
C++类继承和派生类
#include<iostream>
#include<string>
class father {
public:
protected:
private:
};
class son1 :public father {
public:
protected:
};
class son2 :protected father {
public:
protected:
};
class son3 :private father {
public:
protected:
};
int main()
{
father fr;
son1 s1;
return 0;
}
权限:
#include<iostream>
#include<string>
using namespace std;
class father {
public:
int a1=01;
void print()
{
cout << name << "father\t" << money << endl;
}
string getwife()
{
return wife;
}
protected:
int a2=02;
string name="F";
int money=100000;
private:
int a3=03;
string wife = "securet\n";
};
class son1 :public father {
public:
void print()
{
cout << name << "Son \t" << money << endl;
cout << getwife()<<endl;
}
protected:
private:
};
class son2 :protected father {
public:
void print() {
cout << name << " protected " << money << endl;
}
int num1 = 100;
protected:
private:
};
class son3 :private father {
public:
void print()
{
cout << name << " privated\t" << money << endl;
cout << a1 << '\t' << a2 << '\t'<< endl;
}
protected:
};
int main()
{
father fr;
fr.print();
fr.a1;
son1 s1;
s1.a1;
s1.print();
son2 s2;
cout << s2.num1 << endl;
s2.print();
son3 s3;
s3.print();
return 0;
}
继承中的构造函数
#include<iostream>
#include<string>
using namespace std;
class Parent {
public:
Parent(string Fname,string Sname):Fname(Fname),Sname(Sname){}
Parent() { cout << "父类无参构造函数" << endl; };
protected:
string Fname;
string Sname;
private:
};
class son : public Parent {
public:
son() { cout << "这是子类无惨构造函数,优先调用父类无惨构造函数" << endl; }
son(string Fname, string Sname, string sonSname):Parent(Fname,Sname)
{
this->sonFname = Fname;
this->sonSname = sonSname;
}
void print()
{
cout << "Father:" << Fname + Sname << endl;
cout << "SOn :" << sonFname + sonSname << endl;
}
protected:
string sonFname;
string sonSname;
};
int main()
{
son s{"白","江江","慕慕"};
s.print();
return 0;
}
多继承: // 有无参构造 !无父类的必须调用 父类构造函数 并且用初始化参数列表
#include<iostream>
#include<string>
using namespace std;
class MM {
public:
MM(string Fmmname, string Smmname) {
this->Fmmname = Fmmname;
this->Smmname = Smmname;
}
protected:
string Fmmname;
string Smmname;
};
class GG{
public:
GG(string Fggname, string Sggname)
{
this->Fggname = Fggname;
this->Sggname = Sggname;
}
protected:
string Fggname;
string Sggname;
};
class Girl:public MM,public GG{
public:
Girl(string Fggname, string Sggname, string Fmmname, string Smmname)
:MM(Fmmname, Smmname), GG(Fggname, Sggname)
{
this->Fgname = Fggname+Fmmname;
this->Sgname = Sggname+Smmname;
}
void print()
{
cout << "父亲:"<<Fggname + Sggname << endl;
cout << "母亲:"<<Fmmname + Smmname << endl;
cout << "女儿:"<<Fgname + Sgname << endl;
}
protected:
string Fgname;
string Sgname;
};
int main()
{
Girl girl("欧", "阳", "多", "吉");
girl.print();
return 0;
}
经验:各位 张三给孩子起名好听一点, 名字不要太短;
数据成员同名
- 成员函数名同名
类内 不做特别处理OR this,就近原则 类名限定 - 正常赋值调用
- 非正常赋值调用
#include <iostream>
using namespace std;
class M
{
public:
M(string name, int age) :name(name), age(age) {}
void print()
{
cout << "M:";
cout << name << "\t" << age << endl;
}
protected:
string name;
int age;
};
class Girl :public M
{
public:
Girl(string name, int age) :M("父类", 28), name(name), age(age) {}
void print()
{
cout << name << "\t" << age << endl;
cout << M::name << "\t" << M::age << endl;
M::print();
}
protected:
string name;
int age;
};
int main()
{
Girl girl("girl", 18);
girl.print();
M m("mm", 28);
m.print();
Girl* pG = new Girl("newGirl", 19);
pG->print();
pG->M::print();
M* pM = new M("newM", 29);
pM->print();
M* pMM = new Girl("newGirl", 49);
pMM->print();
return 0;
}
####### 贴代码 虚继承:---->菱形继承
#include <iostream>
using namespace std;
class A
{
public:
A(int a) :a(a) {}
protected:
int a;
};
class B :virtual public A
{
public:
B(int a, int b) :A(a), b(b) {}
protected:
int b;
};
class C :virtual public A
{
public:
C(int a, int c) :A(a), c(c) {}
void printC()
{
cout << a << endl;
}
protected:
int c;
};
class D :public C, public B
{
public:
D() :B(1, 2), C(3, 4), A(999)
{
}
void print()
{
cout << a << endl;
cout << B::a << endl;
cout << C::a << endl;
printC();
}
protected:
};
int main()
{
D d;
d.print();
0;
}
总结
- 构造 顺序:
- 因为子类在父类基础上构建,所以需要有 父类 构造才能 构造子类构造函数
+析构顺序: 计算机里拆房子要遵循流程,一步一步拆; 从 屋顶(子类)开始拆
子类对象初始化父类指针:
对象:比作财富 类 :比作需求思想 + 子类财富可以满足 父类
父类对不能象初始化子类指针:
(有专门的方法后面将《可以说说思想教育》) + 父辈财富不能完全满足青年,所以我们要努力 ————————人生苦短,我看看美女
|