#include<iostream> using namespace std; class AA{ int n; public: AA(int k):n(k){} int get(){return n;} int get() const{return n+1;}} int main() {AA a(5); const AA b(6); cout<<a.get()<<b.get(); return 0;}
时间: 2024-01-05 19:02:36 浏览: 18
这段代码定义了一个名为AA的类,该类有一个私有成员变量n和两个公有成员函数get(),一个是普通成员函数,一个是常量成员函数,它们分别返回n和n+1。在main函数中,先创建了一个对象a,传入构造函数参数5,然后调用get()函数输出a的n值,再创建一个常量对象b,传入构造函数参数6,因为b是常量对象,所以只能调用常量成员函数get(),输出b的n+1值。最终输出:56。
相关问题
#include <iostream> using namespace std; class A { public: A(int aa){a=aa;} void show()const{cout<<"A:"<<a<<endl;} int a; }; class B: public A { public: B(int aa,int bb):A(aa),b(bb){ } B(A &ra); //写出该转换构造函数的类外定义 void show()const; private: int b; }; void B::show()const { A::show(); cout<<"B:"<<b<<endl; } void f(const B &rb) { rb.show(); } int main() { A obj(5); f(obj); return 0; }
该程序存在问题,因为在调用 f 函数时,传入的是 A 类型的对象 obj,而 f 函数的参数类型是 const B&,这是不兼容的,编译器会报错。
修改方法如下:
1. 将 f 函数的参数类型改为 const A&,这样就可以接受 A 类型的对象。
2. 在 B 类中添加一个 A 类型的引用作为参数的转换构造函数,将 A 类型的对象转换为 B 类型的对象。
修改后的程序如下:
```c++
#include <iostream>
using namespace std;
class A {
public:
A(int aa){a=aa;}
void show()const{cout<<"A:"<<a<<endl;}
int a;
};
class B: public A {
public:
B(int aa,int bb):A(aa),b(bb){ }
B(A &ra):A(ra),b(0){ } //转换构造函数
void show()const;
private:
int b;
};
void B::show()const {
A::show();
cout<<"B:"<<b<<endl;
}
void f(const A &ra) {
B b(ra); //将 A 类型的对象转换为 B 类型的对象
b.show();
}
int main() {
A obj(5);
f(obj);
return 0;
}
```
经过修改后,程序可以正常运行,输出为:
```
A:5
B:0
```
#include<iostream> using namespace std; class Base { int a; public: Base(int aa=0){a=aa;} void print(){cout<<a<<" ";} }; class Dervied :public Base { int b; public: Dervied(int aa=1,int bb=2):Base(aa) { b=bb; } void print() { cout<<b<<" "; } }; int main() { Dervied d1; Base *p=&d1; d1.print(); p->print(); return 0; }
这是一个简单的 C++ 代码,其中定义了两个类:Base 和 Derived。Derived 是从 Base 派生出来的,也就是说 Derived 类继承了 Base 类的成员变量和成员函数。
在 main 函数中,先定义了一个 Derived 类的对象 d1,然后定义了一个指向 Base 类的指针 p,并将 p 指向 d1 对象。接着分别调用了 d1 的 print 函数和 p 指向的对象的 print 函数。
需要注意的是,Derived 类中也定义了一个名为 print 的函数,它和 Base 类中的 print 函数同名,但参数列表不同。这种情况被称为函数重载。在调用 d1 的 print 函数时,会优先调用 Derived 类中的 print 函数,而不是从 Base 继承过来的 print 函数。而在调用 p 指向的对象的 print 函数时,由于 p 的类型是 Base 类型,因此只能调用从 Base 继承过来的 print 函数,不能调用 Derived 类中的 print 函数。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)