首页 > 其他分享 >实验二 类和对象(2)

实验二 类和对象(2)

时间:2022-10-18 14:24:48浏览次数:34  
标签:const string 对象 double void Complex 实验 c1

一.实验任务4

1.源代码:

Complex.hpp:

#pragma once
#include<cmath>
#include<iostream>
using namespace std;

class Complex{
public:
    Complex(double m=0,double n=0){real=m;imag=n;}
    Complex(const Complex& c1);
    ~Complex()=default;
    double get_real() const;
    double get_imag() const;
    void show() const;
    void add(Complex const &c1);
    friend Complex add(Complex const&c1,Complex const&c2);
    friend bool is_equal(Complex const&c1,Complex const&c2);
    friend double abs(Complex const&c1);
    
private:
    double real;
    double imag;
    
};

Complex::Complex(const Complex&c1){
    real=c1.real;imag=c1.imag;
}

double Complex::get_real() const{
return real;
}

double Complex::get_imag() const{
return imag;
}

void Complex::show() const{
if(imag==0)
cout<<real;
else if(imag<0)
cout<<real<<"-"<<abs(imag)<<"i";
else cout<<real<<"+"<<imag<<"i";

}

void Complex::add(Complex const&c1) {
real+=c1.real;
imag+=c1.real;
}

Complex add(Complex const&c1,Complex const&c2)
{
    Complex c3;
    c3.real=c1.real+c2.real;
    c3.imag=c1.imag+c1.imag;
    return c3;
}
bool is_equal(Complex const&c1,Complex const&c2){
if(c1.real==c2.real&&c1.imag==c2.imag) return true;
else return false;
}

double abs(Complex const&c1){
    return sqrt(c1.real*c1.real+c1.imag*c1.imag);
}

2.task4.cpp:

#include "complex.cpp"
#include <iostream>

// 类测试
void test() {
    using namespace std;

    Complex c1(3, -4);
    const Complex c2(4.5);
    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();
}

2.运行调试:

更换数据后:

 

二.实验任务5

1.源代码:

User.hpp:

#pragma once
#include<iostream>
#include<string>

using namespace std;
class User{
public:
User(string name0,string passwd0="111111",string email0="");
void set_email();
void change_passwd();
void print_info();static int print_n();
private:
string name;
string passwd;
string email;
static int count;
};
User::User(string name0,string passwd0,string email0)
{
    name=name0;passwd=passwd0;email=email0;
    
}

void User::set_email(){
cout<<"Enter email adress:";
cin>>email;
cout<<"succeed"<<endl;
}

void User::change_passwd(){
string passwd1;int flag=1,num=1;
cout<<"Please enter your old passwd:";
while(flag){
cin>>passwd1;
if(passwd1==passwd){
    cout<<"Please enter your new passwd:";
    cin>>passwd;
    cout<<"succeed"<<endl;flag=0;}
else if(num<3) {
cout<<"please enter your old passwd again:";
flag=1;num++;}
else {
cout<<"Plaese try again later"<<endl;
flag=0;
}}}

void User::print_info(){
int i;
cout<<"name:"<<name<<endl;
cout<<"passwd:";
for(i=1;i<=passwd.length();i++)
{
cout<<"*";
}
cout<<"email:"<<email;
;
}
int User::print_n(){
static int x = 1;
    x ++;
    cout << "there are " << x << " users." << endl;
    return x;
}

 

task5.cpp:

#include "task5 User.hpp"
#include <iostream>
// 测试User类
void test()
{
    using std::cout;
    using std::endl;

    cout << "testing 1......\n";
    User user1("Jonny", "92197", "[email protected]");
    user1.print_info();

    cout << endl
         << "testing 2......\n\n";

    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    cout << endl;
    User::print_n();
}

int main()
{
    test();
}

2.运行调试:

 

 

标签:const,string,对象,double,void,Complex,实验,c1
From: https://www.cnblogs.com/xiaoshuai66/p/16802404.html

相关文章

  • 实验二 类和对象
    实验四.cpp#include"Complex.hpp"#include<iostream>voidtest(){usingnamespacestd;Complexc1(7,-8);constComplexc2(7.8);Complexc......
  • 实验6:开源控制器实践——RYU
    一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Desktopamd6......
  • 实验6: 开源控制器实践——RYU
    开源控制器实践——RYU一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境U......
  • JavaScript的日期对象的使用Date
    1<script>2letdate=newDate();//返回的是当前的时间3console.log(date.getFullYear());//获取的当前年份4console.log(date.getMonth()+1);......
  • 面向对象
    面向对象编程面向对象指以属性和行为的观点去分析现实中的事务在面向对象时只分析该对象是否具备该属性和行为类名当由多个单词构成首字母必须大写当成员变量由多个单......
  • 实验一
    #include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");return0;} #include<stdio.h>intmain(){printf("......
  • 面向对象语言的三大特性:封装,继承,多态
    封装:可以将事物的属性和行为抽象出来,封装在一个类中。继承:子类可以从基类上继承其(全部或部分)属性和函数。多态:多态是指一个接口,对应多种实现。C++的多态性具体体现在编译......
  • [答疑]商品的规格是不是应该建模为值对象
    阿华2018-11-2821:59咨询下各位,商品的规格是不是应该建模为值对象?这样对他们的增删不会影响到其他地方。比如一个酒品有200ml,500ml两种规格,管理员后来改成了500ml和700ml,......
  • [答疑]多个对象(红圈)在EA中怎么画出来的
    lihongwei(62***407)14:39:02多个对象(红圈)在EA中怎么画出来的?潘加宇(3504847)16:13:55这个画不出来,如果要表示这个是多个,右击对象,Advance→Multiplicity......
  • 《MiniPRO H750开发指南》第五十六章 串口IAP实验
    第五十六章串口IAP实验​IAP,即在应用编程,通俗地说法就是“程序升级”。产品阶段设计完成后,在脱离实验室的调试环境下,如果想对产品做功能升级或BUG修复会十分麻烦,如果硬件支......