北邮自考C++程序设计(实践)考试题目和答案

北邮自考C++程序设计(实践)考试题目和答案
上机摄像头考试
在这里插入图片描述
1、题目一答案

#include <iostream>
using namespace std;

class Complex
{
public:
	float real,imag;
public:
	Complex();
	Complex(float r,float i){real=r;imag=i;}
	Complex operator +(Complex &x)
	{
		Complex y;
		y.real=real+x.real;
		y.imag=imag+x.imag;
		return y;
	};
};
Complex::Complex(){real=0,imag=0;}

int main(int args, const char * argv[]) {
	Complex c1(1,2),c2(3,4);
	cout << (c1+c2).real << endl; // 输出结果为4
	return 0;
}

2、题目二答案

#include <iostream>
using namespace std;
// const double PI = 3.14159; 本题不需要
class Shapes // 抽象类
{
protected:
	int x ,y;
public:
	void setvaule(int d,int w=0){x=d;y=w;}
	virtual int area() = 0;
};

class Circle:public Shapes{
public:
	int r;
public:
	Circle(int t = 0){ // 构造函数
		r = t;
	}
	int area(){
		return r*r;
	}
};
class Triangle:public Shapes{
public:
	int a,b,c;
	Circle circle;
public:
	Triangle(int x,int y,int z, int w){ //构造函数
		a = x;
		b = y;
		c = z;
		circle = w;
	}
	int area(){
		return a*b*c;
	}
};

int main() {
	Circle c = Circle(2);
	Triangle t = Triangle(1,2,3,4);
	cout << c.area()+ t.area() + t.circle.area() << endl; //输出结果 26
}

按照这个提交的答案;不足之处请谅解

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值