6.
#include<iostream>
using namespace std;
class A
{public:
void f1(){cin>>i;A::f2();}
protected:
void f2(){cout<<"i="<<i<<endl;}
private:
int i;
};
class B:public A
{public:
void f3(){cin>>k;cout<<"k="<<k<<endl;cin>>m;cout<<"m="<<m<<endl;}
int k;
private:
int m;
};
class C:protected B
{public:
void f4(){cin>>n;cout<<"n="<<n<<endl;cin>>p;cout<<"p="<<p<<endl;}
protected:
int n;
private:
int p;
};
class D:private C
{public:
void f5(){cin>>q;cout<<"q="<<q<<endl;cin>>r;cout<<"r="<<r<<endl;}
protected:
int q;
private:
int r;
};
int main()
{
A a1;
B b1;
C c1;
D d1;
a1.f1();
b1.f3();
c1.f4();
d1.f5();
return 0;
}
类的范围 | f1 | f2 | i | f3 | k | m | f4 | n | p | f5 | q | r |
基类A | 公用 | 保护 | 私有 | |||||||||
公用派生类B | 公用 | 保护 | 不可访问 | 公用 | 公用 | 私有 | ||||||
保护派生类C | 保护 | 保护 | 不可访问 | 保护 | 保护 | 不可访问 | 公用 | 保护 | 私有 | |||
私有派生类D | 私有 | 私有 | 不可访问 | 私有 | 私有 | 不可访问 | 私有 | 私有 | 不可访问 | 公用 | 保护 | 私有 |
(1)在派生类外,可以通过对象调用f5函数,其他成员均不能访问。
(2)派生类D的成员函数f5可以访问基类A的成员f1和f2,派生类B的成员f3和k,派生类C的成员f4和n,派生类D的成员q和r。
(3)派生类C的成员函数f4可以访问基类A的成员f1和f2,派生类B的成员f3和k,派生类C的成员n和p。
(4)派生类B的成员函数f3可以访问基类A的成员f1和f2,派生类B的成员k和m。
(5)基类A的成员函数f1可以访问基类A的成员f2和i。
7.
#include<iostream>
using namespace std;
class A
{public:
A(){a=0;b=0;}
A(int i){a=i;b=0;}
A(int i,int j){a=i;b=j;}
void display(){cout<<"a="<<a<<"b="<<b;}
private:
int a;
int b;
};
class B:public A
{public:
B(){c=0;}
B(int i):A(i){c=0;}
B(int i,int j):A(i,j){c=0;}
B(int i,int j,int k):A(i,j){c=k;}
void display1()
{display();
cout<<"c="<<c<<endl;
}
private:
int c;
};
int main()
{
B b1;
B b2(1);
B b3(1,3);
B b4(1,3,5);
b1.display1();
b2.display1();
b3.display1();
b4.display1();
}
8.
#include<iostream>
using namespace std;
class A
{public:
A(){cout<<"constructing A"<<endl;}
~A(){cout<<"destructing A"<<endl;}
};
class B:public A
{public:
B(){cout<<"constructing B"<<endl;}
~B(){cout<<"destructing B"<<endl;}
};
class C:public B
{public:
C(){cout<<"constructing C"<<endl;}
~C(){cout<<"destructing C"<<endl;}
};
int main()
{
C c1;
return 0;
}
9.
#include<iostream>
using namespace std;
class Teacher
{public:
Teacher(string nam,int a,char s,string tit,string ad,string t):
name(nam),age(a),sex(s),title(tit),addr(ad),tel(t){}
void display();
protected:
string name;
int age;
char sex;
string title;
string addr;
string tel;
} ;
void Teacher::display()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"title:"<<title<<endl;
cout<<"address:"<<addr<<endl;
cout<<"name:"<<name<<endl;
cout<<"tel:"<<tel<<endl;
}
class Cadre
{public:
Cadre(string nam,int a,char s,string p,string ad,string t):
name(nam),age(a),sex(s),post(p),addr(ad),tel(t){}
void display();
protected:
string name;
int age;
char sex;
string post;
string addr;
string tel;
};
void Cadre::display()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"post:"<<post<<endl;
cout<<"address:"<<addr<<endl;
cout<<"name:"<<name<<endl;
cout<<"tel:"<<tel<<endl;
}
class Teacher_Cadre:public Teacher,public Cadre
{public:
Teacher_Cadre(string nam,int a,char s,string tit,string p,string ad,string t,float w);
void show();
private:
float wage;
};
Teacher_Cadre::Teacher_Cadre(string nam,int a,char s,string tit,string p,string ad,string t,float w):
Teacher(nam,a,s,tit,ad,t),Cadre(nam,a,s,p,ad,t),wage(w){}
void Teacher_Cadre::show()
{
Teacher::display();
cout<<"post:"<<Cadre::post<<endl;
cout<<"wages:"<<wage<<endl;
}
int main()
{
Teacher_Cadre teacher_Cadre1("Zhang-sang",45,'f',"assistance","president","136 Shanghai Road","(021)6829159",13987.5);
teacher_Cadre1.show();
return 0;
}
标签:f2,cout,私有,成员,谭浩强,课后,派生类,习题,string
From: https://blog.csdn.net/2301_79046256/article/details/140382191