首页 > 其他分享 >试验2 类与对象(2)

试验2 类与对象(2)

时间:2022-10-19 11:35:52浏览次数:45  
标签:real const 对象 double imag 试验 Complex void

试验任务4

Complex.cpp


#pragma once
#include<iostream>
#include<cmath>
class Complex {


public:
Complex();
Complex(double newreal);
Complex(double newreal, double newimag);
Complex(const Complex& C);
double get_real() const;
double get_imag()const;
void show()const;
void add(const Complex &a);


friend Complex add(const Complex& a, const Complex& b);
friend bool is_equal(Complex& a, const Complex& b);
friend double abs(const Complex& a);
private:
double real;
double imag;


};


Complex::Complex() :real(0), imag(0) {}
Complex::Complex(double newreal) :real(newreal), imag(0) {}
Complex::Complex(double newreal, double newimag) :real(newreal), imag(newimag) {}
Complex::Complex(const Complex& C) {
real = C.real;
imag = C.imag;
}


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


}
double Complex::get_imag() const {
return imag;
}
void::Complex::show()const {
std::cout << real << imag ;
if (imag != 0)
std::cout << "i";
}
void Complex::add(const Complex &a) {

real = real + a.real;
imag = imag + a.imag;



}


Complex add(const Complex& a, const Complex& b) {
Complex c;
c.real = a.real + b.real;
c.imag = a.imag + b.imag;


return c;


}


bool is_equal(Complex& a, const Complex& b) {
if (a.real == b.real && a.imag == b.imag)
return true;
else
return false;


 


}
double abs(const Complex&obj) {



return sqrt(obj.real * obj.real + obj.imag * obj.imag);



}

 

试验任务4 源码

#include <iostream>
#include "Complex.hpp"
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();
}

运行结果

试验任务5User.hpp

#pragma once
#include<string>
#include<iostream>
using namespace std;
class User {
public:
    User(string h,string p,string k) {
        name = h;
        passwd = p;
        email = k;
        n++;
    };
    User(string h) {
        n++;
        name = h;
        passwd = "111111";
        email ="";
    }
    void set_email();
    void change_passwd() ;
    void print_info();
    static void print_n();
    static int n;
private:
    string name;
    string passwd;
    string email;

};
int User::n;

void User::print_info() {
    

    cout << "name:   " << name << endl;
    cout << "passwd: " <<"******" << endl;
    cout << "email:  " << email << endl;
}

void User::change_passwd() {
    string h;
    int i;
    cout << "Enter old passworld:";
    for (i = 0; i < 3; i++)
    {
        cin >> h;
        if (h == passwd)
        {
            cout << "Enter new passworld:";
                cin >> passwd;
                cout << "new passworld is set successfully..."<<endl;
                break;
        }
        else {
            if(i<=1)
            cout << "passworld input error.please re-enter again:";
        }
    }
    if (i == 3)
        cout << "passworld input error.please try afer a while"<<endl;

}
void User::set_email() {

    cout << "Enter email address:";
    cin >> email;
    cout << "email is set successfully..." << endl;

}
 void User::print_n(){

     cout << "There are " << n << " users"<<endl;


}

试验任务5 源码

#include "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();
}

 

 

 

 

实验总结

对于基本的语法使用还不太熟练

注意const static  的使用方法

 

标签:real,const,对象,double,imag,试验,Complex,void
From: https://www.cnblogs.com/030227rzxx/p/16804647.html

相关文章

  • ADO.NET中的五个主要对象
    Connection物件Connection对象主要是开启程序和数据库之间的连结。没有利用连结对象将数据库打开,是无法从数据库中取得数据的。这个物件在ADO.NET的最底层,我们可以自......
  • 203. 面向对象(上) 方法重载
    203.面向对象(上)方法重载方法的重载(overload)定义:在同一个类中,允许存在一个以上的同名方法,只要它的参数个数或参数类型不同即可."两同一不同":同一个类,相......
  • 实验二 类与对象(2)
     Task4:Complex.hpp 1#pragmaonce23//Complex类的定义4#include<iostream>5#include<string>6#include<math.h>78usingstd::cout;9usin......
  • 实验2 类和对象(2)
    4.实验任务4Complex.hpp#pragmaonce#include<iostream>#include<math.h>classComplex{private:doublereal,imag;public:Complex();~Com......
  • 实验2 类与对象(2)
    2022.10.12OOP实验课实验2类与对象(2)任务4Complex.hpp#pragmaonce#include<iostream>#include<math.h>classComplex{private:doublereal;double......
  • 实验二 类和对象
    实验任务4task4.cpp#include"Complex.hpp"#include<iostream>//类测试voidtest(){usingnamespacestd;Complexc1(1,-8);constComplexc2(6......
  • 实验二 类和对象
    #pragmaonce#include<iostream>#include<iomanip>#include<cmath>usingstd::cout;usingstd::endl;usingstd::setfill;usingstd::setw;usingstd::left;......
  • 类和对象(2)
    实验4:Complex.hpp文件源码1#pragmaonce23//Complex类的定义4#include<iostream>56usingnamespacestd;78//Complex类的声明9classComp......
  • 实验二 类和对象(2)
    实验任务4h.pp1#pragmaonce2#include<iostream>3#include<cmath>4usingstd::cout;5usingstd::endl;67classComplex{8private:9doub......
  • JavaScript学习--Array数组对象
    定义1.var变量名=newArray(元素列表);如vararr=newArray(1,2,3);2.常用:var变量名=[元素列表];如vararr=[1,2,3];访问arr[索引]=值;如arr[0]=1;ps:数组长度类型均可变 len......