實驗報告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修飾的變數,有了這樣的習慣以後,犯錯誤的情況就會更少。