class Person
{
protected:
string _name = "snowdragon";
size_t _age = 18;
};
class Student :public Person
{
public:
string _id;
};
int main()
{
Student s;
Person p = s;
Person* pp = &s;
Person& rp = s;
//s = p;
Student* ps1 = (Student*)pp;
ps1->_id = "123";
cout << ps1->_id << endl;
pp = &p;
Student* ps2 = (Student*)pp;
//下方为越界访问
//ps2->_id = "123";
//cout << ps2->_id << endl;
return 0;
}
class Person
{
public:
void Func()
{
cout << "Person::void Func()" << endl;
}
protected:
string _name = "snowdragon";
size_t _age = 18;
};
class Student :public Person
{
public:
void Print()
{
cout << "姓名:" << _name << endl;
cout << "年龄:" << _age << endl;
cout << "id:" << _id << endl;
}
void Func()
{
Person::Func();
cout << "Student::void Func()" << endl;
}
protected:
string _name = "snow";
string _id = "123";
};
int main()
{
Student s;
s.Print();
s.Func();
return 0;
}
class Person
{
public:
Person(const char* name = "snowdragon")
:_name(name)
{
cout << "Person()" << endl;
}
Person(const Person& p)
:_name(p._name)
{
cout << "Person(const Person& p)" << endl;
}
Person& operator=(const Person& p)
{
cout << "Person& operator=(const Person& p)" << endl;
if (this != &p)
_name = p._name;
return *this;
}
~Person()
{
cout << "~Person()" << endl;
}
protected:
string _name;
};
class Student :public Person
{
public:
Student(const char* name = "snow", const char* id = "123")
:Person(name)
,_id(id)
{
cout << "Student()" << endl;
}
Student(const Student& s)
:Person(s)
,_id(s._id) //不能在函数体内赋值
{
cout << "Student(const Student& s)" << endl;
}
Student& operator=(const Student& s)
{
cout << "Student& operator=(const Student& p)" << endl;
if (this != &s)
{
Person::operator=(s);
_id = s._id;
}
return *this;
}
~Student()
{
cout << "~Student()" << endl;
}
protected:
string _id;
};
int main()
{
Student s1("dragon", "18");
cout << endl;
Student s2(s1);
cout << endl;
Student s3("snowdragon", "19");
cout << endl;
s1 = s3;
cout << endl;
return 0;
}
class Student;
class Person
{
public:
friend void Print(const Person& p, const Student& s);
protected:
string _name = "snowdragon";
private:
string _sex = "man";
};
class Student :public Person
{
public:
string _hairColor = "black";
protected:
string _id = "123";
private:
size_t _age = 18;
};
void Print(const Person& p, const Student& s)
{
cout << "姓名:" << p._name << endl;
cout << "性别:" << p._sex << endl;
cout << "头发颜色:" << s._hairColor << endl;
/*cout << "id:" << s._id << endl;
cout << "年龄:" << s._age << endl;*/
}
int main()
{
Person p;
Student s;
Print(p, s);
return 0;
}
对于继承关系,一个派生类只有一个直接的基类。
对于继承关系,一个派生类同时继承多个(两个或以上)基类。
属于多继承的一种特殊情况,两个不同的派生类继承于同一个基类,又有一个派生类继承于这个两个派生类。
class Person
{
public:
string _p = "snowdragon";
};
class PerDer1 :public Person
{
protected:
string _pd1 = "snow";
};
class PerDer2 :public Person
{
protected:
string _pd2 = "dragon";
};
class Derive :public PerDer1, public PerDer2
{
protected:
string _d = "snowdragon";
};
int main()
{
Derive d;
//d._p = "sd";
d.PerDer1::_p = "snow sd";
d.PerDer2::_p = "sd dragon";
return 0;
}
class Person
{
public:
string _p = "snowdragon";
};
class PerDer1 :virtual public Person
{
protected:
string _pd1 = "snow";
};
class PerDer2 :virtual public Person
{
protected:
string _pd2 = "dragon";
};
class Derive :public PerDer1, public PerDer2
{
protected:
string _d = "snowdragon";
};
int main()
{
Derive d;
d._p = "sd";
d.PerDer1::_p = "snow sd";
d.PerDer2::_p = "sd dragon";
return 0;
}
class Person
{
public:
int _p;
};
class PerDer1 :public Person
//class PerDer1 :virtual public Person
{
public:
int _pd1;
};
class PerDer2 :public Person
//class PerDer2 :virtual public Person
{
public:
int _pd2;
};
class Derive :public PerDer1, public PerDer2
{
public:
int _d;
};
int main()
{
Derive d;
d.PerDer1::_p = 1;
d.PerDer2::_p = 2;
d._pd1 = 3;
d._pd2 = 4;
d._d = 5;
return 0;
}
本文到这里就结束了,如有错误或者不清楚的地方欢迎评论或者私信
创作不易,如果觉得博主写得不错,请务必点赞、收藏加关注