C++课程设计类作业2

不要问我一个晚上在干啥,就写写这种烦到极点的类,啰嗦!

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 class complexed
 4 {
 5 public:
 6     complexed();
 7     complexed(double real);
 8     complexed(double real,double imag);
 9     void display();
10     void set(double r,double i);
11 private:
12     double real,imag;
13 };
14 complexed::complexed()
15 {
16     set(0.0,0.0);
17     cout<<"default constructor.\n";
18 }
19 complexed::complexed(double real)
20 {
21     set(real,0.0);
22     cout<<"construct called.\n";
23 }
24 complexed::complexed(double real,double imag)
25 {
26     set(real,imag);
27     cout<<"constructor :real="<<real<<",imag="<<imag<<endl;
28 }
29 void complexed::display()
30 {
31     if(imag<0)
32         cout<<real<<imag<<'i'<<endl;
33     else
34         cout<<real<<'+'<<imag<<'i'<<endl;
35 }
36 void complexed::set(double r,double i)
37 {
38     real=r;
39     imag=i;
40 }
41 int main()
42 {
43     complexed c1;
44     complexed c2(6.8);
45     complexed c3(5.6,7.9);
46     c1.display();
47     c2.display();
48     c3.display();
49     c1=complexed(1.2,3.4);
50     c2=5;
51     c3=complexed();
52     c1.display();
53     c2.display();
54     c3.display();
55 }
复制代码

posted @   Angel_Kitty  阅读(490)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示
哥伦布
14°
14:09发布
哥伦布
14:09发布
14°
大雨
南风
3级
空气质量
相对湿度
93%
今天
中雨
14°/19°
周日
中雨
5°/19°
周一
1°/11°