实验1
设计一个复数类
#ifndef Complex_HPP #define Complex_HPP #include<iostream> #include<cmath> using namespace std; class Complex{ private: double real; double imag; public: Complex() { real = 0; imag = 0; } Complex(double i, double j=0) :real{ i }, imag{ j }{real = i; imag = j; } Complex(const Complex& c); ~Complex() = default; double get_real()const; double get_imag()const; void show(); Complex add(Complex &c); friend bool is_equal(Complex& c1, Complex& c2);//bool类型 friend int abs(Complex &c);//取模,数学函数sqrt(),数据类型的转换 }; 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() { if(imag>0||imag==0) cout << real << "+" << imag << "i" << endl;//虚部正负的表示:3+4i与3-4i; else { cout << real << imag << "i"<< endl; } } Complex Complex::add(Complex& c)//有点问题 { Complex c0; c0.real=real + c.real; c0.imag=imag + c.imag; return c0; } bool is_equal(Complex& c1, Complex& c2) { if (c1.imag == c2.imag && c1.real == c2.real) { return true; } else { return false; } } int abs(Complex &c) { double x, y; x = c.real; y = c.imag; return static_cast<int>(sqrt(x * x + y * y)); } #endif #include "Complex.hpp" #include<iostream> int main() { using namespace std; Complex c1(3, -4); Complex c2(4.5);//??为什么要加const?? Complex c3(c1); cout << "c1="; c1.show(); cout << "c2="; c2.show(); cout << "c2.imag=" << c2.get_imag() << endl; cout << "c3="; c3.show(); cout << "abs(c1) ="; cout << abs(c1) << endl;//abs()要写参数表?? cout << boolalpha; cout << "c1==c3:" << is_equal(c1, c3) << endl; cout << "c1==c2:" << is_equal(c1, c2) << endl; Complex c4; c4 = c1.add(c2);//c4 = add1(c1, c2); cout << "c4=c1+c2="; c4.show(); c1.add(c2); cout << "c1+=c2," << "c1="; c1.show(); }

设计一个用户类
#ifndef USER_HPP #define USER_HPP #include<iostream> #include<string> #include<iomanip> using namespace std; class User { private: string name; string password; string s; static int count; public: User(string name0, string password0 = "111111", string s0 = " ") :name{ name0 },password { password0 }, s{ s0 } { name = name0; password = password0; s = s0; ++count; } ~User() = default; void set_email(); void change_passwd(); void print_info()const; void print_n() { cout << "there are:" << count << endl<<"users."; } }; int User::count = 0; void User::set_email() { cout << "Please enter email address:"; cin >> s; cout << endl; cout << "email is set successfully!"<<endl; } void User::change_passwd() { string a,b; int i; cout << "Please enter old password:"; cin >> a; if(a==password) { cout << "Please enter new password:"; cin >> b; if(a!=b)//不能使用 strcmp(a, b);strcmp(const char *str1,const char *str2); { cout << "new passwd is set successfully!"<<endl; } } else { for (i = 1; i < 3; i++) { cout << "Error,please enter old password again:"; cin >> a; } cout << "please try it after!" << endl; } } void User::print_info()const { cout << "name:" << name << endl; cout << "passwd:" << "******" << endl; cout << "email:" << s << endl; } #endif #include "User.hpp" #include<iostream> int main() { using namespace std; cout << "testing 1……" << endl; //User user1("Jonny","92197", "xyz@hotmail.com"); User user1("Jonny", "92197", "xyz@hotmail.com"); user1.print_info(); cout << "testing 2……" << endl; User user2("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); }

总结:
设计一个复数类实验:1.复数的虚部的表示(>0,<0)
2.add()如何实现:两个复数相加得到一个新的复数、把一个复数加给自身
3.取模
4.friend友元函数的使用
设计一个复数类用户类实验:1.string(char a[]?)
2.static

浙公网安备 33010602011771号