首页 > 其他分享 >实验二 类与对象

实验二 类与对象

时间:2022-10-15 20:12:22浏览次数:58  
标签:const string 对象 void int Complex 实验 cout

实验2.1

#include <iostream>
#include <complex>

int main() {
    using namespace std;

    complex<double> c1{ 3, 4 }, c2{ 4.5 };
    const complex<double> c3{ c2 };

    cout << "c1 = " << c1 << endl;
    cout << "c2 = " << c2 << endl;
    cout << "c3 = " << c3 << endl;
    cout << "c3.real = " << c3.real() << endl;
    cout << "c3.imag = " << c3.imag() << endl;

    cout << "c1 + c2 = " << c1 + c2 << endl;
    cout << "c1 - c2 = " << c1 - c2 << endl;
    cout << "abs(c1) = " << abs(c1) << endl;  

    cout << boolalpha;  
    cout << "c1 == c2 : " << (c1 == c2) << endl;
    cout << "c3 == c2 : " << (c3 == c2) << endl;

    complex<double> c4 = 2;
    cout << "c4 = " << c4 << endl;
    c4 += c1;
    cout << "c4 = " << c4 << endl;

}

实验2.2

#include <iostream>
#include <array>

template<typename T>
    void output1(const T& obj) {
      for (auto i : obj)
          std::cout << i << ", ";
      std::cout << "\b\b \n";
}
template<typename T>
    void output2(const T& obj) {
      for (auto p = obj.cbegin(); p != obj.cend(); ++p)
         std::cout << *p << ", ";
      std::cout << "\b\b \n";
}
int main() {
    using namespace std;
    array<int, 5> x1; 
    cout << "x1.size() = " << x1.size() << endl;
    x1.fill(42); 
    x1.at(0) = 999;
    x1.at(4) = -999; 
    x1[1] = 777;
    cout << "x1: ";
    output1(x1);
    cout << "x1: ";
    output2(x1);
    array<double, 10> x2{ 1.5, 2.2 }; 
    cout << "x2.size() = " << x2.size() << endl;
    cout << "x2: ";
    output1(x2);
    array<int, 5> x3{ x1 };  
    cout << boolalpha << (x1 == x3) << endl;
    x3.fill(22);
    cout << "x3: ";
    output1(x3);
    swap(x1, x3); 
    cout << "x1: ";
    output1(x1);
    cout << "x3: ";
    output1(x3);
}

实验2.3

#pragma once
#include <iostream>
#include <string>
#include <iomanip>
using std::string;
using std::cout;
using std::endl;
using std::setfill;
using std::setw;
using std::left;
using std::right;
using std::to_string;
struct Date {
    int year;
    int month;
    int day;
};
class Employee
{
public:
    Employee();
    Employee(string name0, double salary0, int y, int m, int d = 1);
    void set_info(string name0, double salary0, int y, int m, int d = 1);
    
    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); 
public:
    static void display_count(); 
private:
    string id; 
    string name; 
    double salary; 
    Date hire_date; 
public:
    static const string doc; 
private:
    static int count;
};
const string Employee::doc{ "a simple Employee class" };
int Employee::count = 0;
// 默认构造函数
Employee::Employee() : id{ to_string(count + 1) } {
    ++count;
}
// 带参数的构造函数
Employee::Employee(string name0, double salary0, int y, int m, int d) :
    id{ to_string(count + 1) }, 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 << left << setw(15) << "id: " << id << endl;
    cout << setw(15) << "name: " << name << endl;
    cout << setw(15) << "salary: " << salary << endl;
    cout << setw(15) << "hire_date: " << hire_date.year << "-";
    cout << right << setfill('0') << setw(2) << hire_date.month << "-" <<
        setw(2) << hire_date.day;
    cout << setfill(' '); // 恢复到默认空格填充
}
// 更新薪水
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";
}
#include "Employee.h"
#include <iostream>
void test() {
    using std::cout;
    using std::endl;
    cout << Employee::doc << endl << endl;
    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); // 提成15%
    employee2.display_info();
    cout << endl << endl;
    Employee::display_count();
}
int main() {
    test();
}

实验2.4

#pragma once
#include<iostream>
#include<cmath>
using namespace std;
class Complex {
    friend Complex add(const Complex& a,const Complex& b);
    friend bool is_equal(const Complex& a,const Complex& b) ;
    friend float abs(Complex& C);
public:
    Complex(float re=0, float im=0) :real(re), imag(im) {};
    Complex(const Complex& C);

    float get_real() const{
        return real;
    };
    float get_imag() const{
        return imag;
    };
    void show() const;
    Complex add(const Complex& C);

private:
    float real, imag;
};

//复制构造函数接口
Complex::Complex(const Complex &C) {
    real = C.real;
    imag = C.imag;
};

//成员函数show()接口
void Complex::show()const{
    if(imag>0)
    cout << real << '+' << imag << 'i';
    else if(imag<0)
    cout << real  << imag << 'i';
    else
    cout << real;
};

//成员函数add()接口
Complex Complex::add(const Complex &C){
real += C.real, imag += C.imag; return Complex(real + C.real, imag + C.imag); }; //友元函数add()接口 Complex add(const Complex& a, const Complex& b){ return Complex(a.real + b.real, a.imag + b.imag); }; //友元函数is_equal()接口 bool is_equal( const Complex& a, const Complex& b){ if (a.real == b.real && a.imag == b.imag) return true; else return false; } //友元函数abs()接口 float abs(Complex& C) { return sqrt(C.real * C.real+C.imag*C.imag); } #include "Complex.h" #include <iostream> // 类测试 void test() { 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; } int main() { test(); }

 

实验2.5

#pragma once
#include <iostream>
#include <string>
#include <iomanip>
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() const;
    static void print_n();
private:
    string name;
    string passwd;
    string email;
    static int n;
};
int User::n = 0;
//print_n()接口
 void User::print_n(){
    cout << "There are " << n << " users.";
}
//print_info()函数接口
void User::print_info() const{
    cout << left<<setw(8)<<"name:" << name << endl;
    cout <<setw(8)<< "passwd:";
    for (int i = 1; i <= passwd.length(); i++)
        cout << "*";
    cout << endl;
    cout <<setw(8)<< "email:" << email << endl;
}
//set_email()函数接口
void User::set_email() {
    string a;
    cout << "Enter email address:";
    cin >> a;
    email = a;
    cout << "email is set successfully..." << endl;
}
//change_passwd()函数接口
void User::change_passwd() {
    string a;
    for (int i = 1; i <= 3; i++) {
        if (i == 1)
            cout << "Enter old password:";
        cin >> a;
        if (passwd == a) {
            cout << "Enter new passwd:";
            cin >> a, passwd = a;
            cout << "new passwd is set sucessfully..."<<endl;
            break;
        }
        else{
            if(i<3)
            cout << "password input error.Please re-enter again:";
        }
        if (i == 3) {
            cout << "password input error.Please try after a while" << endl; break;
        }
    }
}
#include "User.h"
#include <iostream>

void test() {
    using std::cout;
    using std::endl;
    cout << "testing 1......\n";
    User user1("Jonny", "92197", "[email protected]");
    user1.print_info();

    cout << endl<< "testing 2......\n";
    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    cout << endl;
    User::print_n();

}
int main() {
    test();
}

 

标签:const,string,对象,void,int,Complex,实验,cout
From: https://www.cnblogs.com/xdt2/p/16794903.html

相关文章

  • 实验1
    实验任务1#include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");printf("\n");printf("O\n");printf("<......
  • 类和对象
    #include"complex.hpp"#include<iostream>voidtest(){usingnamespacestd;Complexc1(6,6);constComplexc2(6);Complexc3(c1);c......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的1.能够理解POX控制器的工作原理;2.通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;3.......
  • SDN第四次实验
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ub......
  • 对象数组:用于维护类的多个对象
    对象数组创建数组,来维护多个对象。将对象存到数组的语法:类名[]数组名=new类名[];例如:Studentstu1=newStudent();Studentstu2=newStudent();Studentstu3=new......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验要......
  • 【 云原生 | kubernetes 】资源对象 - 容器化守护进程之Daemonset
    DaemonSet简述DaemonSet对象确保所有(或部分)节点运行一个Pod的副本。随着节点被添加到集群中,Pods也被添加到集群中。当节点从集群中移除时,这些Pods将被垃圾收集。删除一......
  • List集合存储学生对象用三种方式遍历
    packagepackage5;importpackage4.Student;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;//List集合存储学生对象用三种方式遍......
  • 面向对象编程
    面向对象编程包语法packagecom.pac1.pac2.pac3packagecompackagepac1{packagepac2{packagepac3{objectScalaPackage{......
  • 缓冲区溢出实验
    本次实验采用环境为Ubuntulinux64操作系统,在实验楼云环境进行实验介绍:缓冲区溢出是指程序试图向缓冲区写入超出预分配固定长度数据的情况。这一漏洞可以被恶意用户利用......