实验报告1

一、实验目的

1. 掌握类的设计、定义、实现和测试

2. 理解和掌握const引用、const修饰形参、const成员函数

3. 理解和掌握友元函数

4. 理解和掌握static成员

5. 掌握以多文件结构组织源码文件的方法

6. 了解C++标准库和现代C++编程

7. 体会和理解面向对象编程与结构化编程在编程解决实际问题时思维方式的不同

 

实验结论:

实验任务3代码:

Complex.hpp:

#include <iostream>

using namespace std;
class Complex
{


public:
Complex(){
real = 0;
imag = 0;
}
Complex(double c1): real(c1), imag(0) {};
Complex(double c1,double c2): real(c1) , imag(c2) {};
Complex(Complex& c3): real(c3.real), imag(c3.imag) {};
double get_real() const;
double get_imag() const ;
void show() const ;
void add(const Complex& c1);

friend Complex add(const Complex& c1,const Complex& c2);
friend bool is_equal(const Complex& c1, const Complex& c2) ;
friend double abs(Complex& c1);
private:
double real;
double imag;
};

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

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

void Complex::show() const
{
cout << real << "+ " << imag << "i" << endl;
}

void Complex::add(const Complex& c1)
{
real += c1.real;
imag += c1.imag;
}

Complex add(const Complex& c1,const Complex& c2)
{
Complex temp(0,0);
temp.real = c1.real + c2.real;
temp.imag = c1.imag + c2.imag;
return temp;
}

bool is_equal(const Complex& c1, const Complex& c2)
{
return (c1.real == c2.real && c1.imag == c2.imag);
}

double abs(Complex& c1)
{
return c1.real*c1.real + c1.imag*c1.imag;
}

 

实验结果:

 

 更换数据之后的结果:

 

 

实验4源代码 User.hpp部分:

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;
class User
{
private:
    string name;
    string passwd;
    string email;
    static int num;
public:
    User(string a) : name(a), passwd("111111"), email("") { cout << "there are " << ++num << " users\n"; };
    User(string a, string b, string c) : name(a), passwd(b), email(c) { cout << "there are " << ++num << " users\n"; };
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n();
};

int User::num = 0; //注意!这里依然要有类型声明
void User:: set_email()
{
    cout << "please enter your new e-mail:)\n";
    string a;
    cin >> a;
    int count = 0;
    for (unsigned int i = 0; i < a.size(); i++)
    {
        if (a[i] == '@' ||a[i] == '.' || a[i] == 'c')
            count++;
    }
    if (count == 1)
        email = a;
    else
    {
        cout << "email entered error! bye!\n";
        exit(OVERFLOW);
    }
}

void User::change_passwd()
{
    cout << "please enter your new password:)\n";
    string a;
    cin >> a;
    if (a.size() >= 6)
        passwd = a;
    else
    {
        cout << "passwd entered error!bye\n";
        exit(OVERFLOW);
    }
}

void User::print_info()
{
    cout << "name:  " << name << endl;
    cout << "passwd: " << endl;
    unsigned int i = 0;
    while (i++ < passwd.size())
    {
        cout << "*";
    }
    cout << endl;
    cout << "eamil: " << email << endl;
}

void User::print_n()
{
    cout << "there are " << User::num << "accounts in this group!\n";
}

实验结果:

实现密码检查:当检测到密码不足6位时,系统提示错误,程序退出

 

 实验总结:

语法层面:

1、const限定符的使用:通过在成员函数后面加上后置限定符const,如 void print_in() const,可以将成员指针变为const指针,从而起到保护数据的作用。

2、friend修饰符的使用:通过添加友元,可以实现不同类,类与函数之间的数据共享,能够很大程度上的便利编程的过程,同时,也可以拓展oop的思维。

3、默认构造函数:一般情况下,尽量使用在大括号内构造的默认构造函数,如果使用在效果号内提供参数默认值的方法来构造函数,那么再次使用该方法构造的时候,应该注意参数的个数,防止重定义

 

开发层面:

1、使用oop编程的一个经常遇到的问题是:当别人写好函数,准备调用其他编程人员写好的接口时,常常会因为接口的定义不够全面而导致无法进行编译,所以在编写类的时候,就应该想好可能遇到的所有情况,但是仅仅思考所有情况依然不够,还要保持良好的编程习惯,例如:在不影响函数功能的情况下,尽量添加const限定符,一是可以保护数据,二是相当于将函数的接口拓展到了const修饰的变量和非const修饰的变量,有了这样的习惯以后,犯错误的情况就会更少。

posted @ 2021-10-20 21:08  Rtianzhao  阅读(116)  评论(3)    收藏  举报