实验一 类与对象
2021/10/25 6:11:58
本文主要是介绍实验一 类与对象,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Complex.hpp
#ifndef COMPLEX_HPP #define COMPLEX_HPP #include <iostream> #include <cmath> #include <iomanip> class Complex { public: Complex(double real0 = 0, double imag0 = 0) :real(real0), imag(imag0) { } ~Complex() = default; Complex(const Complex& p); double get_real()const; double get_imag()const; void show()const; void add(Complex c); friend Complex add(Complex c1, Complex c2); friend bool is_equal(Complex c1, Complex c2); friend double abs(Complex c); private: double real; double imag; }; Complex::Complex(const Complex& p) :real(p.real), imag(p.imag) { } double Complex::get_real()const{ return real; } double Complex::get_imag()const{ return imag; } void Complex::show()const { if(imag>0) std::cout << real << " +" << imag << "i" ; else if(imag<0) std::cout << real << " "<< imag << "i" ; else std::cout << real ; } void Complex::add(Complex c) { real = real + c.real; imag = imag + c.imag; } Complex add(Complex c1, Complex c2) { Complex c3; c3.real = c1.real + c2.real; c3.imag = c1.imag + c2.imag; return c3; } bool is_equal(Complex c1, Complex c2) { if (c1.real == c2.real && c1.imag == c2.imag) return true; else return false; } double abs(Complex c) { double s; s = sqrt(c.real * c.real + c.imag * c.imag); return s; } #endif
task3.cpp
#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; }
User.hpp
#ifndef USER_HPP #define USER_HPP #include <iostream> #include <string> using namespace std; class User{ public: User(string name0, string passwd0 = "111111", string email0 = " ") :name(name0), passwd(passwd0), email(email0) { n++; } void set_email(); void change_passwd(); void print_info(); static void print_n() { cout << "there are " << n << " users." << endl; } private: string name; string passwd; string email; static int n; }; int User::n = 0; void User::set_email() { string e; cout << "Enter email address: "; cin >> e; email = e; cout << "email is set successfully..." << endl; } void User::change_passwd() { string p1, p2, p3, p; cout << "Enter old passwd: "; cin >> p1; if (p1 == passwd) { cout << "Enter new passwd: "; cin >> p; passwd = p; cout << "new passwd is set successfully..." << endl; } else { cout << "psssword input error. Please re-enter again: "; cin >> p2; if (p2 == passwd) { cout << "Enter new passwd: "; cin >> p; passwd = p; cout << "new passwd is set successfully..." << endl; } else { cout << "psssword input error. Please re-enter again: "; cin >> p3; if (p3 == passwd) { cout << "Enter new passwd: "; cin >> p; passwd = p; cout << "new passwd is set successfully..." << endl; } else cout << "psssword input error. Please try after a while" << endl; } } } void User::print_info() { cout << "name: " << name << endl; cout << "passwd: ******" << endl; cout << "email: " << email << 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(); }
实验总结:
实验一加深了对友元函数及const的理解与运用
实验二是对于string类的简单认识
这篇关于实验一 类与对象的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-102025 蛇年,J 人直播带货内容审核团队必备的办公软件有哪 6 款?
- 2025-01-10高效运营背后的支柱:文档管理优化指南
- 2025-01-10年末压力山大?试试优化你的文档管理
- 2025-01-10跨部门协作中的进度追踪重要性解析
- 2025-01-10总结 JavaScript 中的变体函数调用方式
- 2025-01-10HR团队如何通过数据驱动提升管理效率?6个策略
- 2025-01-10WBS实战指南:如何一步步构建高效项目管理框架?
- 2025-01-10实现精准执行:团队协作新方法
- 2025-01-10如何使用工具提升活动策划团队的工作效率?几个必备工具推荐
- 2025-01-10WiX 标签使用介绍:打造专业安装程序的利器