实验一

complex.hpp

#include<iostream>
#include"complex.cpp"
int main()
{
using namespace std;
complex c1(1,-10);
const complex c2(6.8);
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;
}

complex#ifndef COMPLEX_HPP

#define COMPLEX_HPP
#include"complex.cpp"
using namespace std;
class complex
{
public:
complex(float p,float q);
complex(float s) ;
complex();
complex(const complex&c);
float get_real();
float get_real() const;
float get_imag() ;
float get_imag()const;
void show();
void show() const;
void add(const complex&e);
friend complex add(const complex&x,const complex&y);
friend bool is_equal(complex&e,complex&r);
friend bool is_equal(const complex&e,const complex&r);
friend float abs(complex&w);
private:
float a,b;
};
complex::complex():a(0),b(0){}
complex::complex(float p,float q)
{
a=p;
b=q;
}
complex::complex(float s)
{
a=s;
b=0.0;
}
complex::complex(const complex&c)
{
a=c.get_real();
b=c.get_imag();
}
void complex::show()
{
cout<<a<<b<<"i";
}
void complex::show() const
{
cout<<a;
}
float abs(complex&w)
{
return sqrt(w.a*w.a+w.b*w.b);
}
float complex::get_real()
{
return a;
}
float complex::get_imag()
{
return b;
}
float complex::get_real() const
{
return a;
}
float complex::get_imag() const
{
return b;
}
bool is_equal(complex&e,complex&r)
{
if(e.a==r.a&&e.b==r.b)
return 1;
else
return 0;
}
bool is_equal(const complex&e,const complex&r)
{
if(e.a==r.a&&e.b==r.b)
return 1;
else
return 0;
}
complex add(const complex&x,const complex&y)
{
complex c3;
c3.a =x.a+y.a;
c3.b = x.b+y.b;
return c3;
}
void complex::add(const complex&e)
{
a = a + e.get_real();
b = b + e.get_imag();
}
#endif

实验一

 

#include<iostream>
#include<string>
using namespace std;
class user
{
public:
user(string a);
user(string name,string passwd,string email);
user();
user(user&u);
void set_name();
void set_email() ;
void change_passwd();
void print_info();
static void print_n();
private:
string name,passwd,email;
static int n;
};
int user::n=0;
user::user(string a):name(a),passwd("111111"),email(){n++;}
user::user(user&u)
{
name=u.name;
passwd=u.passwd ;
email=u.email;
}
user::user(string name,string passwd,string email){
this->name=name;
this->passwd=passwd;
this->email=email;
n++;
}
void user::change_passwd()
{
int i;
string s,t;
cout<<"Enter old password:";
for(i=1;i<=3;i++)
{
cin>>s;
if(s==passwd)
{
cout<<"Enter new passwd:";
cin>>t;
cout<<endl;
cout<<"new passwd is set successfully...";
break;
}
else
{
if(i<3)
{
cout<<"password input erroe.Please re-enter again:";
}
else
{cout<<"password input error.Please try after a while.";
cout<<endl;}
}
}
}
void user::set_email()
{
string email;
cout<<"Enter email address:";
cin>>this->email;
cout<<"Email is set successfully..."<<endl;
}
void user::print_info()
{
cout<<"name:"<<this->name<<endl;
cout<<"passwd:"<<"******"<<endl;
cout<<"email:"<<this->email<<endl;
}
void user::print_n()
{
cout<<"there are "<<n<<" users"<<endl;
}
#include<iostream>
int main()
{
using namespace std;
cout <<"testing 1......"<<endl;
user user1("wby","116002","wby@hotmail.com");
user1.print_info();
cout<<endl
<<"testing 2......"<<endl
<<endl;
user user2("myc");
user2.change_passwd();
user2.set_email();
user2.print_info();
user::print_n();
}

实验一实验一

 

 

 

上一篇:10.20


下一篇:实验1 类与对象