类和对象
#include "complex.hpp"
#include <iostream>
void test() {
using namespace std;
Complex c1(6, 6);
const Complex c2(6);
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;
}
int main() {
test();
}
#pragma once
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;
class Complex
{
public:
Complex(double r = 0.0, double i = 0.0) : real{r}, imag{i} {}
Complex(const Complex &obj) : real{obj.real}, imag{obj.imag} {}
public:
double get_real() const { return real; }
double get_imag() const { return imag; }
void show() const;
void add(const Complex &obj);
friend Complex add(const Complex &c1, const Complex &c2);
friend bool is_equal(const Complex &c1, const Complex &c2);
friend double abs(const Complex &obj);
private:
double real;
double imag;
};
void Complex::show() const
{
if (imag > 0)
{
cout << real << " + " << imag << "i";
}
else if (imag < 0)
{
cout << real << " - " << abs(imag) << "i";
}
else
{
cout << real;
}
}
void Complex::add(const Complex &obj)
{
real += obj.real;
imag += obj.imag;
}
Complex add(const Complex &c1, const Complex &c2)
{
return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
bool is_equal(const Complex &c1, const Complex &c2)
{
if (c1.real == c2.real && c1.imag == c2.imag)
return true;
return false;
}
double abs(const Complex &obj)
{
return sqrt(obj.real * obj.real + obj.imag * obj.imag);
}

#include "User.hpp"
#include <iostream>
void test()
{
using std::cout;
using std::endl;
cout << "testing 1......\n";
User user1("cfl", "123456", "cfl@qq.com");
user1.print_info();
cout << endl
<< "testing 2......\n\n";
User user2("xl");
user2.change_passwd();
user2.set_email();
user2.print_info();
cout << endl;
User::print_n();
}
int main()
{
test();
}
#pragma once
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
class User
{
public:
User(string n, string p = "111111", string e = "");
void set_email();
void change_passwd();
void print_info();
static void print_n() { cout << "there are " << count << " users" << endl; }
public:
static int count;
private:
string name;
string passwd;
string email;
};
int User::count = 0;
User::User(string n, string p, string e)
{
name = n;
passwd = p;
email = e;
count++;
}
void User::set_email()
{
string s1;
cout << "Enter email address: ";
cin >> s1;
email = s1;
cout << "email is set successfully..." << endl;
}
void User::change_passwd()
{
cout << "Enter old password: ";
int times = 0;
while (times < 3)
{
string s1;
cin >> s1;
if (s1.compare(passwd)==0)
{
cout << "Enter new passwd: ";
string s1;
cin >> s1;
passwd = s1;
cout << "new passwd is set successfully..." << endl;
break;
}
if (times < 2)
cout << "password input error. Please re-enter again:";
times++;
}
if (times >= 3)
cout << "passwd input error. Please try affter a while." << endl;
}
void User::print_info()
{
cout << "name: " << name << endl;
cout << "passwd: ";
string s1(passwd.length(), '*');
cout << s1 << endl;
cout << "email: " << email << endl;
}

浙公网安备 33010602011771号