#include <iostream > using namespace std; class A { public: A() { cout << "A" << endl; } A(double x, double y) { cout << "K" << endl; } ~A() { cout << "~A" << endl; } void SetData(double i) { y = i; x = i*2; cout << "S" << endl; } virtual void Do() = 0; double x, y;}; class B :public A { public: B() { cout << "B" << endl; } ~B() { cout << "~B" << endl; } void Do() { cout << "x+y=" << x+y << endl;} }; int main() { A *pA; B b; pA = &b; pA->SetData(1); pA->Do(); cout << "T" << endl; return 0; }上述代码的功能和具体流程是怎样的
时间: 2023-10-30 08:30:10 浏览: 46
这段代码定义了两个类 A 和 B,其中 A 是一个抽象类,里面包含了构造函数、析构函数、成员变量 x 和 y,以及一个纯虚函数 Do()。B 是 A 的派生类,实现了 A 中的纯虚函数 Do()。
在 main 函数中,首先定义了一个指向 A 类对象的指针 pA,然后创建了一个 B 类对象 b。接着将 b 的地址赋值给指针 pA。这里需要注意的是,由于 B 是 A 的派生类,因此可以将 B 的地址赋值给指向 A 的指针。这就是多态的体现。
接下来调用了 pA 指向的对象的成员函数 SetData(1),该函数将参数 1 赋值给 y,然后计算 x = 2y。最后输出了字母 S,表示函数执行成功。
然后调用了 pA 指向的对象的成员函数 Do(),该函数输出了 x+y 的值。由于 B 类已经实现了 Do() 函数,因此调用的是 B 类中的函数,输出了 x+y 的值。
最后输出了字母 T,表示程序执行结束。在程序结束时,会自动调用对象的析构函数进行清理工作。由于 pA 指向的是 B 类对象,因此会先调用 B 类的析构函数,再调用 A 类的析构函数。输出的结果是:
```
A
B
S
x+y=4
T
~B
~A
```
相关问题
#include <iostream> #include <cstring> #include <cstdlib> using namespace std; class Complex { private: double r,i; public: void Print() { cout << r << "+" << i << "i" << endl; } // 在此处补充你的代码 }; int main() { Complex a; a = "3+4i"; a.Print(); a = "5+6i"; a.Print(); return 0; }
在此处补充的代码如下:
```c++
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Complex {
private:
double r,i;
public:
Complex(const char* str) { // 构造函数,将字符串转换为复数
r = atof(str); // 将字符串中的实部部分转换为 double 类型
char* p = const_cast<char*>(str); // 将 const char* 类型转换为 char* 类型
while (*p != '+' && *p != '-') p++; // 找到字符串中的 + 或 -
i = atof(p); // 将字符串中的虚部部分转换为 double 类型
if (*str == '-') r = -r; // 处理实部为负数的情况
if (*(p+1) == '-') i = -i; // 处理虚部为负数的情况
}
void Print() {
cout << r << "+" << i << "i" << endl;
}
};
int main() {
Complex a("3+4i"); // 使用构造函数初始化 a
a.Print();
a = "5-6i"; // 使用赋值运算符将字符串赋值给 a
a.Print();
return 0;
}
```
这段代码定义了一个复数类 `Complex`,其中有一个构造函数,用于将字符串转换为复数,并且重载了赋值运算符,可以将字符串赋值给复数。在 `main` 函数中,先使用构造函数初始化一个复数 `a`,然后使用赋值运算符将另一个字符串赋值给 `a`,最后输出结果。
#include <string> #include <iostream> using namespace std; class Student { public: void set(int sid, string sname, double sscore) { id = sid; name = sname; score = sscore; } void print( ) { cout<<"学号:"<<id<<endl; cout<<"姓名:"<<name<<endl; cout<<"分数:”<<score= sendl; } private: int id; string name; double score; }; void main ( ) { Student s1, *ps, s2; s1. set (1001, "James", 86); s2. set(1002,"Kate',67); ps=&s2; s1. print); ps->print( ); }
这段代码有一个语法错误,第13行的cout语句中字符串的引号不匹配。应该将该行代码改为:
cout<<"分数:"<<score<<endl;
另外,建议将main函数的返回类型改为int,以符合C++标准。完整的代码如下:
#include <string>
#include <iostream>
using namespace std;
class Student {
public:
void set(int sid, string sname, double sscore) {
id = sid;
name = sname;
score = sscore;
}
void print() {
cout<<"学号:"<<id<<endl;
cout<<"姓名:"<<name<<endl;
cout<<"分数:"<<score<<endl;
}
private:
int id;
string name;
double score;
};
int main() {
Student s1, *ps, s2;
s1.set(1001, "James", 86);
s2.set(1002, "Kate", 67);
ps = &s2;
s1.print();
ps->print();
return 0;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)