# include <iostream.h> class Class { public : Class ( int n = 6 , int m = 7 ) ; ~Class ( ) ; void Set ( int n , int m ) { x = n ; y = m ; } private : int x, y ; } ; Class :: Class( int n, int m) { Set( n, m ) ; cout <<“Construct : x= ”<< x <<“, y= ” << y << endl ; } Class :: ~Class( ) { cout <<“Destruct : x=” << x <<“, y= ” << y << endl ; } void main( ) { Class a ( 5 ) ; Class b ( 1 , 3 ) ; cout <<“exiting…\n ” ; }
时间: 2024-03-31 18:39:11 浏览: 16
这是一个简单的 C++ 程序,定义了一个叫做 Class 的类,类中包括一个构造函数、一个析构函数和一个成员函数 Set。在主函数中,创建了两个 Class 类的对象 a 和 b,分别传入不同的参数。最后输出一行字符串。运行该程序会输出以下结果:
Construct : x=5, y=7
Construct : x=1, y=3
Destruct : x=5, y=7
Destruct : x=1, y=3
exiting…
相关问题
#include<iostream.h> #include<stdlib.h> #include<graphics.h> #include<math.h> #include<string.h> class CPoint {public: int x,y; CPoint(int a=0,int b=0):x(a),y(b){} }; class CShape//图形基类 { COLORREF bkcol,fgcol,txtcol; public: CShape(COLORREF bkc=0):bkcol(bkc){} virtual void Draw()=0; virtual double Area()=0; }; class CRectangle:public CShape//矩形类 {CPoint lt,rb; public: CRectangle(CPoint lefttop,CPoint rightbottom, COLORREF col=0):CShape(col),lt(lefttop),rb(rightbottom){} void Draw() { rectangle(lt.x,lt.y,rb.x,rb.y); } double Area() { return (rb.x-lt.x)(rb.y-lt.y); } }; #define PI 3.1415926 class CCircle:public CShape {CPoint o; int r; public改错并写出正确的完整代码和运行结果
错误已标注:
```cpp
#include<iostream.h> // 错误:应该是 #include<iostream>
#include<stdlib.h>
#include<graphics.h> // 错误:缺少头文件
#include<math.h>
#include<string.h>
class CPoint {
public:
int x,y;
CPoint(int a=0,int b=0):x(a),y(b){}
};
class CShape { // 修改:将图形基类改为抽象类
COLORREF bkcol,fgcol,txtcol;
public:
CShape(COLORREF bkc=0):bkcol(bkc){}
virtual void Draw()=0;
virtual double Area()=0;
};
class CRectangle:public CShape{
CPoint lt,rb;
public:
CRectangle(CPoint lefttop,CPoint rightbottom, COLORREF col=0):CShape(col),lt(lefttop),rb(rightbottom){}
void Draw() {
rectangle(lt.x,lt.y,rb.x,rb.y);
}
double Area() {
return (rb.x-lt.x)*(rb.y-lt.y); // 错误:乘号写成了括号
}
};
#define PI 3.1415926
class CCircle:public CShape {
CPoint o;
int r;
public:
CCircle(CPoint center,int radius, COLORREF col=0):CShape(col),o(center),r(radius){}
void Draw() {
circle(o.x,o.y,r);
}
double Area() {
return PI*r*r;
}
};
int main() {
initgraph(640,480); // 修改:初始化图形界面
CPoint p1(100,100),p2(200,200);
CRectangle rect(p1,p2,RGB(255,0,0));
rect.Draw();
cout << "矩形面积:" << rect.Area() << endl;
CPoint pc(400,300);
CCircle cir(pc,50,RGB(0,255,0));
cir.Draw();
cout << "圆面积:" << cir.Area() << endl;
getch(); // 修改:等待用户按键关闭图形界面
closegraph(); // 修改:关闭图形界面
return 0;
}
```
运行结果:
```
矩形面积:10000
圆面积:7853.98
```
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; class a{ public: int name; int number; void set_name(int ne) { name=ne; } void set_number(int nr) { number=nr; } void show_name() { cout << name; } void show_number() { cout << number; } }; int main() { a a1; a*p; int x; cin>>x; p->set_number(x); p->show_name(); };
在你的代码中,你定义了一个指向类 a 的指针 p,但是你没有为其分配内存空间,因此在使用该指针时会发生未定义行为。你需要使用 new 运算符为该指针分配内存空间,或者直接定义一个类 a 的对象,不使用指针。另外,在 show_name 函数中,你应该输出 name 而不是 number。修改后的代码如下:
```
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
class a{
public:
int name;
int number;
void set_name(int ne) {
name = ne;
}
void set_number(int nr) {
number = nr;
}
void show_name() {
cout << name;
}
void show_number() {
cout << number;
}
};
int main() {
a a1;
int x;
cin >> x;
a1.set_number(x);
a1.show_name();
return 0;
}
```
这个程序的功能为:从标准输入中读取一个整数,将其设置为 a1 的 number 值,然后输出 a1 的 name 值。