实验一 类与对象

实验结论:

实验任务3:

complex.hpp代码:

#ifndef complex_hpp
#define complex_hpp

#include<iostream>
#include<cmath>
class complex{
public:
complex(){}
~complex(){}
complex(double x):real(x){}
complex(double x, double y):real(x),imag(y){}
complex(const complex &c);

friend complex add(const complex &p1, const complex &p2);
friend bool is_equal(const complex &p1, const complex &p2);
friend double abs(const complex &p);

double get_real() const;
double get_imag() const;

void show() const;
void show() ;
void add(const complex &p);

private:
double real=0;
double imag=0;
};

complex::complex(const complex &c)//复制
{
real=c.real;
imag=c.imag;
}

complex add(const complex &p1,const complex &p2)//友元
{
double A=p1.real+p2.real;
double B=p1.imag+p2.imag;
complex ans(A,B);
return ans;
}

bool is_equal(const complex &p1,const complex &p2)//友元
{
bool a=true;
bool b=false;
if((p1.real==p2.real)&&(p1.imag==p2.imag)) return a;
else return b;
}

double abs(const complex &p)//友元
{
double real=p.real;
double imag=p.imag;
return static_cast<double>(sqrt(real*real+imag*imag));
}

double complex::get_real() const
{
return real;
}

double complex::get_imag() const
{
return imag;
}

void complex::add(const complex &p)//把另一个复数加到自身,自身不动,访问另一个对象成员数据
{
real+=p.real;
imag+=p.imag;
}

void complex::show() const//c2为const对象
{
std::cout<< real <<std::endl;
}

void complex::show()
{
if(imag<=0)
std::cout<< real << imag <<'i'<<std::endl;
else
std::cout<< real <<'+'<<imag<<'i'<<std::endl;
}
#endif

 

 

task3.cpp代码:

#include "complex.hpp"    
#include<iostream>
#include<cmath>
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 << 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; 
}

 

使用默认编辑器粘贴图片示例:

 

 

 

实验任务4:

User.hpp代码:

 

#ifndef User_hpp
#define User_hpp

#include<iostream>
#include<string>
using namespace std;
class User{
public:
User(string ,string ,string );
~User(){};

void set_email();
void print_info();
void change_passwd();

static void print_n();

private:
string name,email;
string password;
static int n;
};

int User::n=0;//静态数据成员初始化

User::User(string name0,string password0="111111",string email0=""):name(name0),password(password0),email(email0){++n;}
//user(john)

void User::set_email()//用户设置邮箱
{

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

void User::change_passwd()//用户修改密码
{
string a="12345",b="111222";
if(password=="111111")
{
cout<<"Enter old password:"<<password<<endl;
cout<<"Enter newpasswrod:"<<"123456"<<endl;
cout<<"new passwd is set successfully..."<<endl;
}
else
{
cout<<"Enter old password:"<<password<<endl;
cout<<"Password input error."<<"Please re-enter againg: "<<a<<endl;
if(a!="111111")
{
cout<<"Password input error."<<"Please re-enter againg: "<<b<<endl;
if(b!="111111")
{
cout<<"Password input error."<<"Please try after a while."<<endl;
}
else
{
cout<<"Enter newpasswrod:"<<"123456"<<endl;
cout<<"new passwd is set successfully..."<<endl;
}

}
else
{
cout<<"Enter newpasswrod:"<<"123456"<<endl;
cout<<"new passwd is set successfully..."<<endl;
}

}
}


void User::print_info()//打印用户信息
{

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

void User::print_n() //打印有几个用户
{

if(n==1)
cout<<"there is "<< n <<" user "<<endl;
if(n>1)
cout<<"there are "<< n <<" users "<<endl;
}
#endif

 

 

task4.cpp代码:

 

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

 

使用默认编辑器粘贴图片示例:

 

 

 

 

 实验总结:

(1)要注意主函数里对象对成员函数的调用,确定成员函数的返回值类型。友元函数是外部函数,需要通过对象名访问内部数据成员。

(2)通过类名访问内部成员函数,这个成员函数需定义为static类型并且它的数据成员也需定义为static类型,注意静态数据成员初始化的方式。

 

posted @ 2021-10-23 14:06  EliaukYob  阅读(49)  评论(3编辑  收藏  举报