实验1 C++类与对象

<Complex.hpp>:

#ifndef COMPLEX_HPP
#define COMPLEX_HPP 
#include <iostream>
#include <cmath>
using namespace std;
class Complex{
	public:
	    Complex(){}
        ~Complex(){}
		Complex(double x):real(x){}
		Complex(double x, double y):real(x),imag(y){}
		Complex(const Complex &c);
		
		double get_real() const;
	    double get_imag() const;
	    
	    
		void show() const{
			if(imag>0){
				cout << real << "+" << imag << "i" << endl;
			} else if(imag==0){
				cout << real << endl;
			}else{
				cout << real << imag << "i" << endl;
			}
		} 
	    void add(const Complex& c){
	    	real += c.real;
	    	imag += c.imag;
		};
		
	    friend Complex add(const Complex &c1,const Complex &c2);
	    friend bool is_equal(const Complex &c1,const Complex &c2);
	    friend double ads(const Complex &c1); 
	    
	private:
		double real=0.0;
		double imag=0.0;
};
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;
}

Complex add(const Complex &c1,const Complex &c2){
	Complex c4;
	c4.real = c1.real + c2.real;
	c4.imag = c1.imag + c2.imag;
	return c4;	
}
bool is_equal(const Complex &c1,const Complex &c2){
	return c1.real == c2.real && c1.imag == c2.imag;
}
double ads(const Complex &c1){
	return static_cast<double>(sqrt(c1.real*c1.real + c1.imag*c1.imag));
}
#endif

<task3.cpp>:

#include <iostream>
#include "Complex.hpp"

int main()
{
    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 << ads(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;
}

运行结果:

 

<User.hpp>:

#ifndef USER_HPP
#define USER_HPP
#include <iostream>
#include <string>
#include <string.h>
using namespace std;

class User {
	public:
	User(string x):name(x){++n;}
	User(string x,string y,string z):name(x),password(y),email(z){++n;}
    void set_email(){
	    string email1;
	    cout << "Enter email address: ";
		cin >> email1;
		email = email1;
		cout << "email is set successfully..." << endl;
	};
	void change_password();
	void print_info(){
		cout << "name:    " << name << endl;
		cout << "password:******" << endl;
		cout << "email:   " << email << endl;
	};
	void print_n(){
	   	cout << "there are " << n << " users." << endl;
	};
	private:
    string name;
	string password = "111111";
	string email = " ";  
	int n=0;
};

void User::change_password(){
    string oldpassword;
	cout << "Enter old password: ";
	cin >> oldpassword; 
	
	int k=1;
	while(password!=oldpassword&&k<3){
		cout << "password input error. Please re-enter again: ";
		cin >> oldpassword;
		cout << endl;
		k++;
	}
	if(password==oldpassword){
		cout << "Enter new password:";
		cin >> password;
		cout << "new password is set successfully..." << endl;
	}
	if(k>=3){
		cout << "password input error. Please try after a while." << endl;
	}
}
#endif

<task4.cpp>:

#include "User.hpp"
#include <iostream>
#include <string>
#include <string.h>

int main()
{
    using namespace std;

    cout << "testing 1......" << endl;
    User user1("Jonny", "92197", "xyz@hotmail.com");
    user1.print_info();

    cout << endl
         << "testing 2......" << endl
         << endl;
    User user2("Leonard");
    user2.change_password();
    user2.set_email();
    user2.print_info();

    User::print_n();
}

运行结果:

 

 

posted @ 2021-10-23 16:21  金玉·礼予天地四方  阅读(23)  评论(3编辑  收藏  举报