#include<complex.h> #include<iostream> #include<cmath> int main() { using namespace std; complex<double> c1(3,4); complex<double> c2(4.5); complex<double> c3(c2); cout << " c1 = " << c1 << endl; cout << " c2 = " << c2 << endl; cout << " c3 = " << c3 << endl; cout << " c1 + c2 = " << c1 + c2 << endl; cout << boolalpha;//设置布偶型bool值以true/false方式输出 cout << " c1 == c2 : " << (c1 == c2 ) << endl; cout << "c3 == c2 : " << (c3 == c2 ) << endl; cout << "abs(c1) = " << abs(c1) << endl;//abs()对复数进行取模运算,头文件cmath complex<int> c4(3,4),c5(2,7); cout << "c4 - c5 = " << c4 - c5 << endl; }
#ifndef EMPLOYEE_HPP #define EMPLOYEE_HPP // Employee类的定义 #include <iostream> #include <iomanip> #include <string> using namespace std; struct Date { int year; int month; int day; }; // Employee类的声明 class Employee { public: Employee(); Employee(string name0, double salary0, int y, int m, int d); void set_info(string name0, double salary0, int y, int m, int d); // 设置雇员信息 string get_name() const; // 获取雇员姓名 double get_salary() const; // 获取雇员薪水 void display_info() const; // 显示雇员信息 void update_salary(double s); // 更新雇员薪水 void update_hire_date(int y, int m, int d); // 更新雇佣日期 void raise_salary(double by_percent); // static void display_count(); private: string name; // 雇员姓名 double salary; // 雇员薪水 Date hire_date; // 雇员雇佣日期 static int count; // 用于记录雇员总人数 }; int Employee::count = 0; // 默认构造函数 Employee::Employee() { ++count; } // 带参数的构造函数 Employee::Employee(string name0, double salary0, int y, int m, int d): name{name0}, salary{salary0}, hire_date{y, m, d} { ++count; } // 设置员工信息 void Employee::set_info(string name0, double salary0, int y, int m, int d) { name = name0; salary = salary0; hire_date.year = y; hire_date.month = m; hire_date.day = d; } // 获取员工姓名 string Employee::get_name() const { return name; } // 获取员工薪水 double Employee::get_salary() const { return salary; } // 显示雇员信息 void Employee::display_info() const { cout << "name: " << name << endl; cout << "salary: " << salary << endl; cout << "hire_date: " << hire_date.year << "-" << setfill('0') << setw(2) << hire_date.month << "-" << setw(2) << hire_date.day; } // 更新薪水 void Employee::update_salary(double s) { salary = s; } // 更新雇佣日期 void Employee::update_hire_date(int y, int m, int d) { hire_date.year = y; hire_date.month = m; hire_date.day = d; } // 雇员提薪加成 // by_percent是提升比例 void Employee::raise_salary(double by_percent) { double raise = salary * by_percent / 100; salary += raise; } // 显示雇员总数 void Employee::display_count() { cout << "there are " << count << " employees\n"; } #endif
#include "Employee.hpp" #include <iostream> int main() { using namespace std; Employee employee1; employee1.set_info("Sam", 30000, 2015, 1, 6); employee1.update_hire_date(2017, 6, 30); employee1.update_salary(35000); employee1.display_info(); cout << endl << endl; Employee employee2("Tony", 20000, 2020, 3, 16); employee2.raise_salary(15); employee2.display_info(); cout << endl << endl; Employee::display_count(); }
#include<iostream> #include<cmath> using namespace std; class Complex { public: Complex(int x = 0, int y = 0 );//不能在一个类里面同时声明一个默认构造器,和一个带有默认参数的构造函数; //构造函数不能被申明为const; Complex(const Complex &c); int get_real() ; int get_imag() const; void show() const;//因为c2为const所以如果在这里不声明为const就会放大c2的权限 void add(Complex c) ; private: int real; int imag; friend Complex add(const Complex c1,const Complex c2); friend bool is_equal(const Complex c1,const Complex c2);//友元函数不能被声明为const因为它不属于类里面的东西 //所以当友元函数和构造函数的参数为const时,形参需要被定义为const; friend int abs(const Complex c); }; // Complex::Complex(int x, int y ) //声明中构造函数同时声明缺省参数,而在构造函数的定义中不用再次声明缺省参数。 { real = x; imag = y; } Complex::Complex(const Complex &c) { real = c.real; imag = c.imag; } int Complex::get_real() { return real; } int Complex::get_imag() const { return imag; } void Complex::show() const { cout << real << imag << "i" << endl; } void Complex::add(Complex c) { real += c.real; imag += c.imag; } Complex add(const Complex c1,const Complex c2) { Complex a; a.real = c1.real + c2.real; a.imag = c1.imag + c2.imag; return a; } bool is_equal(const Complex c1,const Complex c2) { if( (c1.real == c2.real) && (c1.imag == c2.imag)) { return true; } return false; } int abs(const Complex c) { int a; a = sqrt( ( c.real) * (c.real) + (c.imag) * (c.imag)); return a; }
#include "Complex.hpp" #include <iostream> 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; return 0; }
#include<iostream> #include<string> using namespace std; class User { public: User(string theName, string thePassword = "111111",string theEmail = ""); void set_email(); void change_passwd(); void print_info(); static void print_n() ; private: string name; string password; string email; static int count; }; User::User(string theName, string thePassword,string theEmail) { name = theName; password = thePassword; email = theEmail; count++; } void User::set_email() { cout << " Enter email address:"; cin >> email; cout << "email is set succesfully\n"; } void User::change_passwd() { string input; cout << "Enter old password:"; cin >> input; int n = 2; while((n != 0) && (input != password)) { n--; cout << "password input error. Please re-enter again:"; cin >> input; } if(input == password) { cout << "Enter new password:"; cin >> password; cout << "new passwd is set successful..." << endl; } if(n == 0) { cout<< "Please try after a while" << endl; } } void User::print_info() { cout << "name: " << name << endl; cout << "passed: ******\n"; cout << "email: " << email << endl; } void User::print_n() { cout << "there are " << count << "uesr.\n"; }
#include "User.hpp" #include <iostream> int User::count = 0;//count是静态成员,为其分配空间需要在主函数的文件内,主函数前进行 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.构造函数不能被申明为const,友元函数不能被声明为const因为它不属于类里面的东西,所以当友元函数和构造函数的参数为const时,形参需要被定义为const;
3.因为c2为const所以如果类的函数不声明为const就会放大c2的权限
4.声明中构造函数同时声明缺省参数,而在构造函数的定义中不用再次声明缺省参数。
5.为静态成员分配内存要在主函数的文件内