第4题:
理解函数重载的概念:
以下为易混淆点
1.派生类中重新定义了一个和基类中的原型完全相同的函数,这种情况不属于函数重载,属于重写
2.类中定义了两个同名、同参数表的函数,但其中一个是常成员函数,这种情况属于函数重载
第14题:
虚基类的正确声明为:class B:virtual public A
第15题:
理解虚函数:
虚函数既可以在函数说明时定义,也可以在函数实现时定义,这种说法错误
第30题:
定义派生类时,若不使用类关键字显式地规定采用何种继承方式,则默认方式为 私有 继承
例子:
#include <iostream.h>
class Point {
private:
int x,y;
public:
Point(int a,int b){x = a;y = b;}
void show(){
cout << "x = " << x << ", y = " << y << endl;
}
};
class a: Point{
private :
int H,W;
public:
a(int a,int b,int h,int w):Point(a,b){
H = h;
W = w;
}
void show(){
Point::show();
cout << "H = " << H << ", W = " << W << endl;
}
};
class Test:public a{
public:
Test(int a,int b,int h, int w):a(a,b,h,w){}
void show(){
a::show()
//Point::show();//error C2247: 'show' not accessible because 'a' uses 'private' to inherit from 'Point'
}
};
void main(){}
第39题:
重载的运算符保持其原有的运算符个数、优先级和结合性不变
第49题:
注意friend与static的区别和用法
例子一:
#include <iostream.h>
#include <math.h>
class dis{
private:
double x,y;
public:
dis(double x1,double y1){
x = x1;
y = y1;
}
double getx(){
return x;
}
double gety(){
return y;
}
friend double log(dis&,dis&);
};
double log(dis&a,dis&b){
double px = a.x - b.x;
double py = a.y - b.y;
return sqrt(px*px + py*py);
}
void main(){
dis p1(3.5,4.5),p2(5.5,6.5);
double d = log(p1,p2);
cout << "the distance is " << d;
}
例子二:
#include <iostream.h>
#include <math.h>
class dis{
private:
double x,y;
public:
dis(double x1,double y1){
x = x1;
y = y1;
}
double getx(){
return x;
}
double gety(){
return y;
}
static double log(dis&,dis&);
};
double dis::log(dis&a,dis&b){
double px = a.x - b.x;
double py = a.y - b.y;
return sqrt(px*px + py*py);
}
void main(){
dis p1(3.5,4.5),p2(5.5,6.5);
double d = dis::log(p1,p2);
cout << "the distance is " << d;
}
第53题:
有两个矩阵a和b,都是2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如c = a + b
#include <iostream.h>
class Mx {
private:
int m[2][3];
public:
Mx();
friend Mx operator+ (Mx&,Mx&);
void input();
void show();
};
Mx::Mx(){
for(int i = 0; i < 2; i++ ) {
for(int j = 0 ; j < 3; j++ ){
m[i][j] = 0;
}
}
}
Mx operator+ (Mx& a, Mx& b){
Mx c;
for(int i = 0 ; i < 2 ; i++){
for(int j = 0 ; j < 3 ; j++){
c.m[i][j] = a.m[i][j]+b.m[i][j];
}
}
return c;
}
void Mx::input(){
cout << "输入矩阵的值:"
<< endl;
for(int i = 0 ; i < 2; i++){
for(int j = 0 ; j < 3 ; j++){
cin >> m[i][j];
}
}
}
void Mx::show(){
for(int i = 0 ; i < 2; i++){
for(int j = 0 ; j < 3; j++){
cout << m[i][j] << " ";
}
cout << endl;
}
}
void main(){
Mx a,b,c;
a.input();
b.input();
cout << endl << "矩阵a:"
<< endl;
a.show();
cout << endl << "矩阵b:"
<< endl;
b.show();
c = a + b;
cout << endl << "矩阵c:"
<< endl;
c.show();
}