C++语言程序设计实验一 类与对象

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++语言程序设计实验一 类与对象的更多相关文章
- 160809208沈昊辰c语言程序设计实验选择结构设计
<C语言程序设计>实验报告 学 号 160809208 姓 名 沈昊辰 专业.班 计科16-2班 学 期 2016-2017 第1学期 指导教师 黄俊莲 吴喆 实验地点 C区二层机房 ...
- 160809209_李梦鑫_C语言程序设计实验3 循环结构程序设计
<C语言程序设计>实验报告 学 号 160809209 姓 名 李梦鑫 专业.班 计科16-2班 学 期 2016-2017 第1学期 指导教师 黄俊莲 吉吉老师 实验地点 C05 ...
- 160809209_李梦鑫_C语言程序设计实验2+选择结构程序设计_进阶
<C语言程序设计>实验报告 学 号 160809209 姓 名 李梦鑫 专业.班 计科16-2班 学 期 2016-2017 第1学期 指导教师 黄俊莲 吴喆 实验地点 C05 机 ...
- C语言程序设计实验报告三
C程序设计实验报告 姓 名:张美盛 实验地点:家 实验时间:2020年3月29日 实验项目:4.3.1 If语句的应用 4.3.2 switch-case的应用 4.3.3 switch-case嵌套 ...
- C语言程序设计实验报告四
C程序设计实验报告 姓 名:赖瑾 实验地点:家 实验时间:2020年4月9日 实验项目:5.3.1练习2 求数列的前n项和 5.3.2练习2 求水仙花数 5.3.4 十进制转换 5.3.5练习1 百马 ...
- C语言程序设计实验报告(第一次实验)
C程序设计实验报告 实验项目:C语言程序设计教程实验1.3.2:1.3.3:1.3.4:2.3.1:2.3.2 姓名:赖瑾 实验地点:家 实验时间:2020.2.25 目录 C程序设计实验报告 一.实 ...
- Apex语言(八)类和对象
1.类和对象 一切皆对象,是客观存在的万物,有标识.属性和行为.一个人,一台电脑,一辆轿车都是对象 类是创建对象的模板或蓝图,是对象的抽象,是对象的类型. 一个对象是一个类的一个实例,是一个类的变量. ...
- 160809209_李梦鑫_C语言程序设计实验2 选择结构程序设计
实验2-1 输入3个数,并按由大到小的顺序输出. 实验要求: 编写一个C程序,输入3个数,并按由大到小的顺序输出. 源码:#include <stdio.h> int main() { i ...
- 符瑞艺 160809228_C语言程序设计实验2 选择结构程序设计
实验2- 输入3个数,并按由大到小的顺序输出. 实验要求: 编写一个C程序,输入3个数,并按由大到小的顺序输出. 参考: 源码: #include <stdio.h> int main() ...
- 学号160809224姓名黄家帅c语言程序设计实验2 选择结构程序设计
实验2-1 输入3个数,并按由大到小的顺序输出. 实验要求: 编写一个C程序,输入3个数,并按由大到小的顺序输出. 源码: #include <stdio.h>void main(){ i ...
随机推荐
- ubunut安装qtcreater
安装gcc 1 kxb@kxb:~$ gcc -v 2 3 Command 'gcc' not found, but can be installed with: 4 5 sudo apt insta ...
- Java时间加减操作
Java时间加减操作 /** * 时间计算 * * @param str 时间字符串 * @param format 时间格式 * @param type Calendar内置常量 * @param ...
- cornerstone4.1破解版 for mac
百度网盘: https://pan.baidu.com/s/1l_0rHMF11mZsUP3qJrp7Uw 密码: 8ei9
- 踩坑纪实----tomcat部署前端服务器不能访问中文文件夹或中文文件名问题
修改tomcat的server.xml文件(解决含有中文的文件.图片的不能下载.显示的问题): 找到下列配置信息在xml文件中的位置,添加黑体字部分的参数即可(disableUploadTimeout ...
- 数学工具类Math-练习
数学工具类Math 概述 java.lang.Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数.类似这样的工具 类,其所有方法均为静态方法,并且不会创建对象,调用起来非常 ...
- 【分析笔记】Linux gpio_wdt.c 看门狗设备驱动源码分析
基本原理 该看门狗的设备驱动实现原理很简单,比较主要的有两点: 一.定时器喂狗 通过定时器根据配置文件配置的喂狗方式(如脉冲切换.电平切换),对指定的 gpio 进行脉冲切换或电平切换实现喂狗. 脉冲 ...
- 深入解读.NET MAUI音乐播放器项目(一):概述与架构
系列文章将分步解读音乐播放器核心业务及代码: 深入解读.NET MAUI音乐播放器项目(一):概述与架构 深入解读.NET MAUI音乐播放器项目(二):播放内核 深入解读.NET MAUI音乐播放器 ...
- vue @click的stop和prevent
@click.stop 阻止事件冒泡 @click.prevent 阻止事件的默认行为 联合饿了吗UI使用的时候,el-table(主表)包含一个或多个子表时(el-tabs),点击右侧的编辑.删除时 ...
- 跟着廖雪峰学python 005
函数的调用.定义.参数 编辑 #######命名关键字参数没完 abs()函数:绝对值 >>> abs(100) 100 >>> abs(-20) 20 ma ...
- SpringBoot集成Tomcat服务
目录 一.Tomcat集成 1.依赖层级 2.自动化配置 二.Tomcat架构 三.Tomcat配置 1.基础配置 2.属性配置类 3.配置加载分析 四.周期管理方法 1.控制类 2.核心方法 五.参 ...