实验一 类与对象
#ifndef Complex_hpp #define Complex_hpp #include<iostream> #include<cmath> using namespace std; class Complex{ public: Complex(); Complex(double a) { real=a; imag=0; } Complex(double a,double b){ real=a; imag=b; } Complex(Complex &c); ~Complex(){} double get_real() const; double get_imag() const; void show(); void show() const; void add(const Complex &c ); friend Complex add(Complex &c1,const Complex &c2); friend bool is_equal(Complex &c1,const Complex &c2); friend double abs(Complex &c1); private: double real,imag; }; Complex::Complex():real(0),imag(0){ } Complex::Complex(Complex &c) { real=c.get_real(); imag=c.get_imag(); } double Complex::get_real() const{ return real; } double Complex::get_imag() const{ return imag; } void Complex::show(){ if(imag>0) cout<<real<<"+"<<imag<<"i"; else if(imag==0) cout<<real; else cout<<real<<imag<<"i"; } void Complex::show() const{ if(imag>0) cout<<real<<"+"<<imag<<"i"; else if(imag==0) cout<<real; else cout<<real<<imag<<"i"; } void Complex::add(const Complex &c){ real+=c.get_real(); imag+=c.get_imag(); } Complex add(Complex &c1,const Complex &c2) { Complex t(0,0); t.real=c1.real+c2.real; t.imag=c1.imag+c2.imag; return t; } bool is_equal(Complex &c1,const Complex &c2) { if(c1.real==c2.real&&c1.imag==c2.imag) return true; else return false; } double abs(Complex &c) { return sqrt(c.real*c.real+c.imag*c.imag); } #endif
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(2, -4); const Complex c2(2.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; }
#ifndef User_hpp
#define User_hpp
#include<iostream>
#include<string>
using namespace std;
class User{
public:
User();
User(string name0,string password0,string email0);
void set_email();
void change_passwd();
void print_info();
static void print_n();
private:
string name;
string password;
string email;
static int n;
};
int User::n=0;
User::User(){
n++;
}
User::User(string name0,string password0="111111",string email0="\0"):name{name0},password{password0},email{email0}
{
n++;
}
void User::set_email(){
cout<<"Enter email address: ";
string email0;
int len,i,flag=0;
cin>>email0;
len=email0.length();
for(i=0;i<len;i++)//判断邮箱合法性
{
if(email0[i]=='@')
flag=1;
if(email0[len-1]!='m'||email0[len-2]!='o'||email0[len-3]!='c'||email0[len-4]!='.')//结尾是.com
flag=0;
}
if(flag==1)
{
email=email0;
cout<<"email is set successfully..."<<endl;
}
else
{
cout<<"illegal email address"<<endl;
email="none email available ";}
}
void User::change_passwd(){
cout<<"Enter old password: ";
string password0,password1;
int count=0;
int flag=0,i;
while(count<3)
{
cin>>password0;
if(password0!=password)
{
count++;
if(count==3)
cout<<"password input error,please try after a while."<<endl;
else
cout<<"password input error,please re-enter again: " ;
}
else
{
cout<<"Enter new passed: ";
cin>>password1;
password=password1;
cout<<"new passwd is set sucessfully..."<<endl;
break;
}
}
}
void User::print_info()
{
cout<<"name: "<<name<<endl;
cout<<"passwd: ******"<<endl;
cout<<"email: "<<email<<endl;
}
void User::print_n()
{
cout<<"there are "<<n<<" user";
if(n>1)
cout<<"s"<<endl;
}
#endif
#include "User.hpp" #include <iostream> 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_passwd(); user2.set_email(); user2.print_info(); User::print_n(); }
实验总结:
实验三要构造的函数不难,但因为没注意到c2是const修饰的让我卡了很长时间。如果函数中含有用const修饰的参数,一定要在形参中加入const修饰。
实验四构造的类还没有实验三的复杂,主要要关注string类在类中的应用。如果对string类有些了解,写起来困难不大。