中易网

C++高手请过来。。

答案:3  悬赏:20  
解决时间 2021-03-14 23:48
请帮忙解答下下面四道题目。。

非常感谢!
1、分析下面程序运行的结果。 #include
using namespace std;
int main()
{
int a,b,c;
a=10;
b=23;
c=a+b;
cout<<〃a+b=〃;
cout<cout<return 0;
2、分析下面的程序,写出其运行时的输出结果。
#include
using namespace std;
class Date
{public:
Date(int,int,int);
Date(int,int);
Date(int);
Date( );
void display( );
private:
int month;
int day;
int year;
};
Date∷Date(int m,int d,int y):month(m),day(d),year(y)
{ }
Date∷Date(int m,int d):month(m),day(d)
{year=2005;}
Date∷Date(int m):month(m)
{day=1;
year=2005;
}
Date∷Date( )
{month=1;
day=1;
year=2005;
}
void Date∷display( )
{cout<int main( )
{
Date d1(10,13,2005);
Date d2(12,30);
Date d3(10);
Date d4;
d1.display( );
d2.display( );
d3.display( );
d4.display( );
return 0;

3、阅读下面程序,分析其执行过程,写出输出结果。
#include
using namespace std;
class Student
{public:
Student(int n,float s):num(n),score(s){ }
void change(int n,float s){num=n;score=s;}
void display( ){cout<private:
int num;
float score;
};

int main( )
{Student stud(101,78.5);
stud.display( );
stud.change(101,80.5);
stud.display( );
return 0;
5. 有以下程序结构,请分析访问属性。
class A // A为基类
{public:
void f1( );
int i;
protected:
void f2( );
int j;
private:
int k;
};

class B: public A //B为A的公用派生类
{public:
void f3( );
protected:
int m;
private:
int n;
};

class C: public B //C为B的公用派生类
{public:
void f4( );
private:
int p;
};

int main( )
{A a1; //a1是基类A的对象
B b1; //b1是派生类B的对象
C c1; //c1是派生类C的对象
return 0;
}问:
(1) 在main函数中能否用b1.i,b1.j和b1.k引用派生类B对象b1中基类A的成员?
(2) 派生类B中的成员函数能否调用基类A中的成员函数f1和f2?
(3) 派生类B中的成员函数能否引用基类A中的数据成员i,j,k?
请写出具体答案
最佳答案
1.实现2个数相加,结果是a+b=33

2.10 13 2005
12 30 2005
10 1 2005
1 1 2005

3.101 78.5
101 80.5
5.(1)不可以,应为b1是a派生类的对象,它不能调基类的成员。
(2)可以调用,应为是共有继承所以可以访问基类中public和protect的成员函数
(3)可以引用基类的i和j但是不能引用k因为k是私有成员,在派生类中是不可见的。
全部回答
1、分析下面程序运行的结果。 #include using namespace std; int main() { int a,b,c; a=10; b=23; c=a+b; //c=33 cout<<〃a+b=〃; cout< using namespace std; class Date { public: Date(int,int,int); Date(int,int); Date(int); Date( ); void display( ); private: int month; int day; int year; }; Date∷Date(int m,int d,int y):month(m),day(d),year(y) { } Date∷Date(int m,int d):month(m),day(d) {year=2005;} Date∷Date(int m):month(m) {day=1; year=2005; } Date∷Date( ) {month=1; day=1; year=2005; } void Date∷display( ) {cout< using namespace std; class Student {public: Student(int n,float s):num(n),score(s){ } void change(int n,float s){num=n;score=s;} void display( ){cout<
  • 2楼网友:甜野猫
  • 2021-03-14 17:55
大学的时候学过,不过现在都忘得差不多了,所以没办法帮你
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯