-
-
继承关系的特殊性-派生类继承了友元函数
p182 程序说明:基类Base的成员函数print()是另一个类another的友元函数,所以再print()中可以访问another的私有成员变量aaa。类Derived是类Base的派生类,他从类Base继承了print()函数。也就是说,通过Derived对象也能调用print()函数访问another的私有成员aaa。 //在主函数中,通过类Base的变量a调用print()函数,调用的… -
派生类改变基类中成员的访问权限
在派生类的成员函数中访问基类的成员时,需要使用类名加限定,如"BaseClass::v1". 基类BaseClass和派生类DerivedClass中都声明了成员变量v1 派生类中的v1重定义了基类中的v1,他们的访问权限是不一样的。 派生类的成员函数既可以访问本类的v1,也可以访问从基类继承的v1. 例如在函数printv()中,即输出了本类v1也输出了基类v1的值。 #in… -
P173-3-2 下面的程序实现复数的加法
#include <iostream> using namespace std; class myComplex { public: int real, imag; myComplex(int r = 0, int i = 0) { real = r; imag = i; } }; myComplex operator+(__________, myComplex& b)//答… -
P174-1 给出下列程序都输出结果
#include <iostream> using namespace std; class pointer { public: int a; int* p; pointer() { a = 100; p = new int(10); } pointer(const pointer& tempp)//复制构造函数 { if (this != &tempp)//避免a=a… -
第三章-程序分析题-4
#include <iostream> using namespace std; class Location { public: int x, y; void init(int initx, int inity); int Getx(); int Gety(); }; void Location::init(int initx, int inity) { x = initx; y =… -
第三章-程序分析题-3
#include <iostream> using namespace std; class exa { int a; public: exa(int b = 5)//这种形参里赋值的用不带参数exa x;也会默认调用他 { a = b++; //a=5 } void print() { a = a + 1;//a=6 cout << a << " &… -
第三章-程序分析题-2
#include <iostream> using namespace std; class CTest { private: int n; public: CTest() { n = 1; } int GetValue() const { return n; } int GetValue() { return 2 * n; } }; int main() { const CTest … -
第三章-程序分析题-1
给出下列程序分析结果 #include <iostream> using namespace std; class base { private: int m; public: base() {}; base(int a):m(a){} int get() { return m; } void set(int a) { m = a; } }; int main() { base* pt… -
为类A编写构造函数,使用均带初始值0的两个参数,为类型成员变量赋初值
三、程序填空题 为类A编写构造函数,使用均带初始值0的两个参数,为类型成员变量赋初值 #include <iostream> using namespace std; class A { int a,b; public: //答案开始: A(int a = 0, int b = 0) { this->a = a; this->b = b; } //答案结束 }; int m… -
以下程序输出的是10,10,请将程序补充完整
三、程序填空题 5.以下程序输出的是10,10,请将程序补充完整 #include <iostream> using namespace std; class f { private: int x, y; public: void f1() { x = 10; y = 10; } void print() { cout << x << ","… -
下面的程序定义了类Test,num初值为5,程序创建对象t且修改其num的值为10,用show()函数输出num的值,请先将程序补充完整。
三、程序填空题 4.下面的程序定义了类Test,num初值为5,程序创建对象t且修改其num的值为10,用show()函数输出num的值,请先将程序补充完整。 #include <iostream> using namespace std; class Test { private: static int num; public: Test(int); void show(); }; … -
下面的程序输出59,请将程序补充完整。
三、程序填空题 3.下面的程序输出59,请将程序补充完整。 #include <iostream> using namespace std; class Aton { //________: 答案:public: int X, Y; int zx, zy; void init(int i, int j) { zx = i; zy = j; } Aton(int i, int j, in… -
下面的程序输出3,5,请将程序补充完整。
三、程序填空题 2.下面的程序输出3,5,请将程序补充完整。 #include <iostream> using namespace std; class point { private: float x, y; public: void f1(float a, float b) { x = a; y = b; } point() { x = 0; y = 0; } void getx… -
下面的程序输出3.3,请将程序补充.
三、程序填空题 1.下面的程序输出3.3,请将程序补充。 #include <iostream> using namespace std; class point { private: float x, y; public: void f(float a) { x = a; } void f() { x = 0; } //__________ max(point& a, poi… -
友元函数实现复数类操作
#include <iostream> using namespace std; class myComplex { private: double real, imag; public: myComplex(); myComplex(double r,double x); friend myComplex addCom(myComplex c1, myComplex c2); //两… -
友元函数声明3-14
分别说明了类Pixel的两个友元函数:一个全局函数getDist()Pixel,pixel;另一个类Test的成员函数printX(Pixel p); 要说明printX(Pixel p)是友元函数,必须要现有类Test的全部定义。而类Test又用到了Pixel所以需要将Pixel在类Test进行声明。 在主函数中调用全局友元函数时直接写函数名就可以。 调用类内部的成员函数时,必须通过类的对象才可… -
封闭类的复制构造函数
3-13复制构造函数的使用: 如果封闭类的对象是用默认复制构造函数初始化的,那么它包含的成员对象也会用复制构造函数初始化 #include <iostream> using namespace std; class A { public: A() { cout << "default" << endl; } A(A& a) { cou… -
封闭类对象的创建与消亡
封闭类对象的创建与消亡p127 #include <iostream> using namespace std; class CTyre //轮胎类 { private: int radius; // 半径 int width; // 宽度 public: CTyre() :radius(16), width(185) { cout << radius << &… -
程序3-11 封闭类的定义
p124封闭类的定义: #include <iostream> using namespace std; class CTyre { private: int radius;//半径 int width;//宽度 public: CTyre() :radius(16), width(185) {};//声明构造函数1 CTyre(int r, int w):radius(r), wid…
幸运之星正在降临...
点击领取今天的签到奖励!
恭喜!您今天获得了{{mission.data.mission.credit}}积分
我的优惠劵
-
¥优惠劵使用时效:无法使用使用时效:
之前
使用时效:永久有效优惠劵ID:×
没有优惠劵可用!