Complex.hpp文件源码:

#include <iostream>

using namespace std;

class Complex {
public:
Complex(float r = 0, float i = 0) : real{ r }, imag{ i }{ }
Complex(const Complex& obj) : real{ obj.real }, imag{ obj.imag }{ }
float get_real() const { return real; }
float get_imag() const { return imag; }
void show() const {
if (imag == 0) {
cout << real;
}
else if(imag < 0){
cout << real << " - " << std::abs(imag) << "i";
}
else {
cout << real << " + " << imag << "i";
}
}
void add(const Complex c2) {
real += c2.real;
imag += c2.imag;
}
friend Complex add(Complex c1, Complex c2) {
Complex c3;
c3.real = c1.real + c2.real;
c3.imag = c1.imag + c2.imag;
return c3;
}
friend bool is_equal(Complex c1, Complex c2) {
bool equal = false;
if (c1.real == c2.real && c1.imag == c2.imag) {
equal = true;
}
return equal;
}
friend float abs(Complex c1) {
float m = 0;
m = std::sqrt(c1.real * c1.real + c1.imag * c1.imag);
return m;
} private:
float real;
float imag;
};

  task3.cpp源码:

#include "Complex.hpp"
#include <iostream>
int main()
{
using namespace std;
Complex c1(4, -1);
const Complex c2(-2);
Complex c3(c1);
cout << "c1 = ";
c1.show();
cout << endl;
cout << "c2 = ";
c2.show();
cout << endl;
cout << "c2.imag = " << c2.get_imag() << endl;
cout << "c3 = ";
c3.show();
cout << endl;
cout << "abs(c1) = ";
cout << abs(c1) << endl;
cout << boolalpha;
cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
Complex c4;
c4 = add(c1, c2);
cout << "c4 = c1 + c2 = ";
c4.show();
cout << endl;
c1.add(c2);
cout << "c1 += c2, " << "c1 = ";
c1.show();
cout << endl;
}

  运行测试结果截图:

  myUser.hpp文件源码 :(由于我的电脑内可能存在User.hpp文件,故使用myUser命名该文件)

#include <iostream>
#include <iomanip> using namespace std; class User {
public:
User(string namestr, string passwdstr = "111111", string emailstr = " ") : name{ namestr }, passwd{ passwdstr }, email{ emailstr }{ n++; }
void set_email() {
string new_email;
cout << "Enter email address: ";
cin >> new_email;
email = new_email;
cout << "email is set successfully\n";
}
void change_passwd() {
string old_passwd, new_passwd;
int count = 0;
cout << "Enter old password: ";
cin >> old_passwd;
while (count < 2) {
if (passwd != old_passwd) {
cout << "password input error. Please re-enter again: ";
cin >> old_passwd;
}
else {
cout << "Enter new password: ";
cin >> new_passwd;
passwd = new_passwd;
cout << "new password is set successfully...\n";
return;
}
count++;
}
if (passwd != old_passwd) {
cout << "password input error. Please try after a while.\n";
}
else {
cout << "Enter new password: ";
cin >> new_passwd;
passwd = new_passwd;
cout << "new password is set successfully...\n";
}
}
void print_info() {
cout << left << setw(8) << "name:" << name << endl;
cout << setw(8) << "passwd:" << "******" << endl;
cout << setw(8) << "email:" << email << endl;
}
void static print_n() {
cout << "there are " << n << " users.";
} private:
string name;
string passwd;
string email;
static int n;
};

  task4.cpp源码:

#include "myUser.hpp"
#include <iostream> int User::n = 0; int main()
{
using namespace std;
cout << "testing 1......" << endl;
User user1("Luna", "46713", "112@hotmail.com");
user1.print_info();
cout << endl
<< "testing 2......" << endl
<< endl;
User user2("Leonard");
user2.change_passwd();
user2.set_email();
user2.print_info();
User::print_n();
}

  运行测试结果截图:

C++语言程序设计实验一 类与对象的更多相关文章

  1. 160809208沈昊辰c语言程序设计实验选择结构设计

    <C语言程序设计>实验报告 学 号 160809208 姓 名 沈昊辰 专业.班 计科16-2班 学    期 2016-2017 第1学期 指导教师 黄俊莲 吴喆 实验地点 C区二层机房 ...

  2. 160809209_李梦鑫_C语言程序设计实验3 循环结构程序设计

    <C语言程序设计>实验报告 学 号 160809209 姓 名 李梦鑫 专业.班 计科16-2班 学    期 2016-2017 第1学期 指导教师 黄俊莲 吉吉老师 实验地点 C05 ...

  3. 160809209_李梦鑫_C语言程序设计实验2+选择结构程序设计_进阶

    <C语言程序设计>实验报告 学 号 160809209 姓 名 李梦鑫 专业.班 计科16-2班 学    期 2016-2017 第1学期 指导教师 黄俊莲 吴喆 实验地点 C05 机 ...

  4. C语言程序设计实验报告三

    C程序设计实验报告 姓 名:张美盛 实验地点:家 实验时间:2020年3月29日 实验项目:4.3.1 If语句的应用 4.3.2 switch-case的应用 4.3.3 switch-case嵌套 ...

  5. C语言程序设计实验报告四

    C程序设计实验报告 姓 名:赖瑾 实验地点:家 实验时间:2020年4月9日 实验项目:5.3.1练习2 求数列的前n项和 5.3.2练习2 求水仙花数 5.3.4 十进制转换 5.3.5练习1 百马 ...

  6. C语言程序设计实验报告(第一次实验)

    C程序设计实验报告 实验项目:C语言程序设计教程实验1.3.2:1.3.3:1.3.4:2.3.1:2.3.2 姓名:赖瑾 实验地点:家 实验时间:2020.2.25 目录 C程序设计实验报告 一.实 ...

  7. Apex语言(八)类和对象

    1.类和对象 一切皆对象,是客观存在的万物,有标识.属性和行为.一个人,一台电脑,一辆轿车都是对象 类是创建对象的模板或蓝图,是对象的抽象,是对象的类型. 一个对象是一个类的一个实例,是一个类的变量. ...

  8. 160809209_李梦鑫_C语言程序设计实验2 选择结构程序设计

    实验2-1 输入3个数,并按由大到小的顺序输出. 实验要求: 编写一个C程序,输入3个数,并按由大到小的顺序输出. 源码:#include <stdio.h> int main() { i ...

  9. 符瑞艺 160809228_C语言程序设计实验2 选择结构程序设计

    实验2- 输入3个数,并按由大到小的顺序输出. 实验要求: 编写一个C程序,输入3个数,并按由大到小的顺序输出. 参考: 源码: #include <stdio.h> int main() ...

  10. 学号160809224姓名黄家帅c语言程序设计实验2 选择结构程序设计

    实验2-1 输入3个数,并按由大到小的顺序输出. 实验要求: 编写一个C程序,输入3个数,并按由大到小的顺序输出. 源码: #include <stdio.h>void main(){ i ...

随机推荐

  1. vsftp安装文档

    vsftp安装文档 张京坤 20190325 ftp安装 安装环境:centOS7.6 安装vsfptd 在线安装:服务器联网状态下 检查是否安装了vsftpd:rpm -qa |grep vsftp ...

  2. 单例 pickle模块

    今日内容 单例模式实现的多种方式 方式一: class C1: __instance = None def __init__(self,name,age): self.name = name self ...

  3. CSS中知

    1CSS特性    1.3优先级    1.4权重叠加计算 2Chrome调试工具     2.1查错流程 3CSS盒子模型    3.1内容的宽度和高度    3.2边框(border)-连写形式 ...

  4. 工作这么多年,我总结的数据传输对象 (DTO) 的最佳实践

    前言 数据传输对象 (DTO) 是一种设计模式,常用于软件开发不同层或者不同系统之间传输数据.DTO 的主要目的是封装数据并防止它被其他层或系统直接访问或修改.通过遵循一组最佳实践,开发人员可以确保他 ...

  5. AJAX容易出错地方,错误处理

    myajax.js   //创建路由器对象 const express=require('express'); //引入连接池模块 const pool=require('../pool.js'); ...

  6. Blazor Hybrid (Blazor混合开发)更好的读取本地图片

    在 Blazor Hybrid 应用中,Razor 组件在设备上本机运行. 组件通过本地互操作通道呈现到嵌入式 Web View 控件. 组件不在浏览器中运行,并且不涉及 WebAssembly. R ...

  7. 默认方法:negate-集合信息筛选

    默认方法:negate "与"."或"已经了解了,剩下的"非"(取反)也会简单.默认方法negate的JDK源代码为︰ 集合信息筛选 数组当 ...

  8. Java 进阶P-6.6+P-7.1

    接口设计模式 接口 接口是纯抽象类 所有的成员函数都是抽象函数 所有的成员变量都是public static final 接规定了长什么样,但是不管里面有什么 实现接口 类用extends,接口用im ...

  9. Python 内置界面开发框架 Tkinter入门篇 丙(文末有福利彩蛋,今天可是元宵节)

    以下内容为本人的学习笔记,如需要转载,请声明原文链接微信公众号「ENG八戒」https://mp.weixin.qq.com/s/B1hH5Qzd2RkAiiUId1tLWw 本文大概 2874 个字 ...

  10. flutter系列之:在flutter中使用导航Navigator

    目录 简介 flutter中的Navigator Navigator的使用 总结 简介 一个APP如果没有页面跳转那么是没有灵魂的,页面跳转的一个常用说法就是Navigator,flutter作为一个 ...