Task 4
hpp文件如下
#pragma once
#include<iostream>
#include<complex>
class Complex
{
public:
Complex();
Complex(double r,double i = 0.0);
Complex(const Complex& obj);
double get_real()const { return real; };
double get_imag()const { return img; };
void show()const;
void add(Complex c1);
friend Complex add(Complex c1,Complex c2);
friend bool is_equal(Complex c1,Complex c2);
friend double abs(Complex c);
private:
double real;
double img;
};
Complex::Complex() :real{ 0 }, img{ 0 } {}
Complex::Complex(double r, double i): real{ r }, img{ i } {}
Complex::Complex(const Complex& obj) :real{ obj.real }, img{ obj.img } {}
void Complex::add(Complex c1){
real += c1.real;
img += c1.img;
}
Complex add(Complex c1, Complex c2) {
Complex c3;
c3.real = c1.real + c2.real;
c3.img = c1.img + c2.img;
return c3;
}
bool is_equal(Complex c1, Complex c2)
{
if (c1.img == c2.img && c1.real == c2.real)
return 1;
else
return 0;
}
double abs(Complex c)
{
double count = 0;
count = sqrt(c.img * c.img + c.real * c.real);
return count;
}
void Complex::show()const{
if (img > 0)
std::cout << real << "+" << img << "i" << std::endl;
else if (img < 0)
std::cout << real << img << "i" << std::endl;
else if (img == 0)
std::cout << real << std::endl;
}
cpp文件如下:
#include "Complex.hpp"
#include <iostream>
// 类测试
void test() {
using namespace std;
Complex c1(3, -4);
const Complex c2(7.8);
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;
}
int main() {
test();
}
运行结果截图
Task 5
hpp文件如下:
#pragma once
#include<iostream>;
#include<string>
class User {
public:
User(std::string name0, std::string password0 = "111111",std::string email0 = "");
void set_email();
void change_passwd();
void print_info();
void static print_n() {
std::cout << "共有 " << n << " 名用户" << std::endl;
}
private:
std::string name;
std::string email;
std::string passwd;
static int n;
};
int User::n = 0;
User::User(std::string name0, std::string password0,std::string email0) :name{ name0 }, email{ email0 }, passwd{ password0 } { n++; }
void User::set_email() {
std::string email1;
std::cout << "请用户用键盘输入邮箱地址!" << std::endl;
std::cin >> email1;
if (email1 != "")
{
email = email1;
std::cout << "邮箱设置成功!" << std::endl;
}
else
std::cout << "未设置邮箱,将使用默认值!" << std::endl;
}
void User::change_passwd() {
std::cout << "请输入旧密码!" << std::endl;
std::string password;
for (int i = 0; i < 3; i++)
{
flag:
std::cin >> password;
if (password != passwd)
{
std::cout << "原密码不正确,请重新输入!";
std::cout << "已连续输入错误" << i + 1 << "次!"<<std::endl;
if (i == 2)
{
std::cout << "已连续输入三次错误密码,请稍后再试。"<<std::endl;
goto flag;
}
}
else if (password == passwd)
{
std::cout << "原密码验证成功,请输入新密码!"<<std::endl;
std::cin >> passwd;
std::cout << "密码修改成功!" << std::endl;
break;
}
}
}
void User::print_info() {
int length = passwd.length();
std::cout << "用户名为:" << name << std::endl;
std::cout << "邮箱为:" << email << std::endl;
std::cout << "密码为:";
while (length--)
{
std::cout << "*";
if (length == 0)
std::cout << std::endl;
}
}
cpp文件如下:
#include "User.hpp"
#include <iostream>
// 测试User类
void test() {
using std::cout;
using std::endl;
cout << "testing 1......\n";
User user1("Li", "123456", "lkx@hotmail.com");
user1.print_info();
cout << endl
<< "testing 2......\n\n";
User user2("lixuan");
user2.change_passwd();
user2.set_email();
user2.print_info();
cout << endl;
User::print_n();
}
int main() {
test();
}
运行结果如下:
标签:real,std,img,对象,double,Complex,实验,c1 From: https://www.cnblogs.com/lkx1366070554/p/16796201.html实验心得
1.对于类与对象的用法概念知识点等加深了认识。
2.对于const等修饰词有了更为深刻的认识。