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

实验2 类与对象

时间:2023-10-22 19:24:26浏览次数:31  
标签:const string 对象 double void int 实验 include

实验一

方式一

t.h

#ifndef T_H 
#define T_H 
#include <iostream> 
#include <string> 
using namespace std; 
class T { 
public: 
T(int x = 0, int y = 0);  
T(const T &t); 
T(T &&t); 
~T(); 
void set_m1(int x); 
int get_m1() const; 
int get_m2() const; 
void display() const;
friend void func(); 
private: 
int m1, m2; 
public: 
static void disply_count();
public: 
static const string doc; 
static const int max_count;
private: 
static int count; 
};
void func(); 
#endif
View Code

t.cpp

#include "t.h"  
const string T::doc{"a simple class"}; 
const int T::max_count = 99; 
int T::count = 0; 
T::T(int x, int y): m1{x}, m2{y} { 
++count; 
cout << "constructor called.\n"; 
}
T::T(const T &t): m1{t.m1}, m2{t.m2} { 
++count; 
cout << "copy constructor called.\n"; 
}
T::T(T &&t): m1{t.m1}, m2{t.m2} { 
++count; 
cout << "move constructor called.\n"; 
}
T::~T() { 
--count; 
cout << "destructor called.\n"; 
}
void T::set_m1(int x) { 
m1 = x; 
}
int T::get_m1() const { 
return m1; 
}
int T::get_m2() const { 
return m2; 
}
void T::display() const { 
cout << m1 << ", " << m2 << endl; 
}

void T::disply_count() { 
cout << "T objects: " << count << endl;  
}
 
void func() { 
T t1; 
t1.set_m1(55); 
t1.m2 = 77; 
t1.display(); 
}
View Code

main.cpp

#include "t.h" 
void test() { 
cout << "T class info: " << T::doc << endl; 
cout << "T objects max_count: " << T::max_count << endl; 
T::disply_count(); 
T t1; 
t1.display(); 
t1.set_m1(42); 
T t2{t1}; 
t2.display(); 
T t3{std::move(t1)}; 
t3.display(); 
t1.display(); 
T::disply_count(); 
}
int main() { 
cout << "============测试类T============" << endl; 
test(); 
cout << endl; 
cout << "============测试友元函数func()============" << endl; 
func(); 
}
View Code

运行结果

 

方式二

t.hpp

#pragma once
#include <iostream>
#include <string>
using namespace std;

class T {
public:
T(int x = 0, int y = 0);
T(const T &t);
T(T &&t); 
~T();
void set_m1(int x);
int get_m1() const;
int get_m2() const;
void display() const;
friend void func();
private:
int m1, m2;
public:
static void disply_count();
public:
static const string doc;
static const int max_count;
private:
static int count;
};

const string T::doc{"a simple class"};
const int T::max_count = 99;
int T::count = 0;

T::T(int x, int y): m1{x}, m2{y} {
++count;
cout << "constructor called.\n";
}
T::T(const T &t): m1{t.m1}, m2{t.m2} {
++count;
cout << "copy constructor called.\n";
}
T::T(T &&t): m1{t.m1}, m2{t.m2} {
++count;
cout << "move constructor called.\n";
}
T::~T() {
--count;
cout << "destructor called.\n";
}
void T::set_m1(int x) {
m1 = x;
}
int T::get_m1() const {
return m1;
}
int T::get_m2() const {
return m2;
}
void T::display() const {
cout << m1 << ", " << m2 << endl;
}

void T::disply_count() {
cout << "T objects: " << count << endl;
}

void func() {
T t1;
t1.set_m1(55);
t1.m2 = 77;
t1.display();
}
View Code

main.cpp

#include "t.hpp"

void test() {
cout << "T class info: " << T::doc << endl;
cout << "T objects max_count: " << T::max_count << endl;
T::disply_count();
T t1;
t1.display();
t1.set_m1(42);
T t2{t1};
t2.display();
T t3{std::move(t1)};
t3.display();
t1.display();
T::disply_count();
}

int main() {
cout << "============测试类T============" << endl;
test();
cout << endl;
cout << "============测试友元函数func()============" << endl;
func();
}
View Code

运行结果

 

实验二

employee.hpp

#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 << std::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;
}

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";
}
View Code

main.cpp

#include "Employee.hpp"
#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(2019, 6, 30);
employee1.update_salary(35000);
employee1.display_info();
cout << endl << endl;
Employee employee2{"Tony", 20000, 2023, 3, 16};
employee2.raise_salary(15);
employee2.display_info();
cout << endl << endl;
Employee::display_count();
}
int main() {
test();
}
View Code

运行结果

 

实验三

t.h

#pragma once
#include <iostream>
#include <cmath>
using namespace std;
class Complex
{
public:
    Complex(double a = 0, double b = 0) :a(a), b(b) {}
    Complex(const Complex& x) ;
    double get_real() { return a; }
    double get_imag() const { return b; }
    void show() const;
    void add(const Complex &z);
    ~Complex(){};
    friend Complex add(const Complex &k,const Complex &l);
    friend bool is_equal(const Complex &asd,const Complex &zxc);
    friend double abs(const Complex &x);

private:
    double a, b;
};
View Code

t.cpp

#include "t.h"
Complex::Complex(const Complex& x)
{
    a = x.a;
    b = x.b;
}
void Complex::show() const
{
    if (b > 0)
        cout << a << "+" << b << "i" ;
    else if (b < 0)
        cout << a << b << "i" ;
    else
        cout << a ;
}
void Complex::add(const Complex &z)
{
    a = a + z.a;
    b = b + z.b;
}
Complex add(const Complex &k,const Complex &l)
{
    Complex s;
    s.a=k.a+l.a;s.b=k.b+l.b;
    return (s);
}
bool is_equal(const Complex &asd,const Complex &zxc)
{
    if (asd.a == zxc.a && asd.b == zxc.b)
        return (true);
    else
        return (false);
}
double abs(const Complex &x)
{
    return(sqrt(x.a * x.a + x.b * x.b));
}
View Code

main.cpp

#include "t.h"
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();
}
View Code

运行结果

 

实验四

User.hpp

#pragma once

#include<iostream>
#include<string>

using namespace std;


class User {
public:
    User(string name,string passwd = "111111",string email = " "): N{name}, P{passwd}, E{email} {n++;}
    ~User() = default;    
 
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n();
 
private:
    string N,P,E;
    static int n;
    
};
 
int User::n = 0;

void User::set_email()
{
    cout << "Enter email address: " ;
    cin >> E ;
    cout << "email is set successfully..." << endl;
}
 
void User::change_passwd()
{
    cout << "Enter old password: ";
    string temp;
    int i = 3;
    while(i)
    {
        cin >> temp;
        if(temp == P)
        {   
            cout << "Enter new passwd: ";
            cin >> P;  
            cout << "new passwd is set successfully..." << endl;
            break;
        }
        else 
        {
            i--;
            if(i != 0)
                cout << "password input error. Please re-enter again: " ;
        }
        if(i == 0)
        {
            cout << "password input error. Please try after a while." << endl;
        }
    }
 
}

void User::print_info()
{   
    string pp(P.size(), '*');
    cout << "name:   " << N << endl;
    cout << "passwd: " << pp << endl;
    cout << "email:  " << E << endl;
}
 
void User::print_n()
{
    cout << "there are " << n << " users." << endl;
}
View Code

task4.cpp

#include "User.hpp"
#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\n";
User user2("Leonard");
user2.change_passwd();
user2.set_email();
user2.print_info();
cout << endl;
User::print_n();
}
int main() {
test();
}
View Code

运行结果

 

 

实验五

account.h

class SavingsAccount {
private:
    int id;
    double balance;
    double rate;
    int lastDate;
    double accumulation;
    static double total;
    
    void record(int date, double amount);
    double accumulate(int date) const {
        return accumulation+balance*(date-lastDate);
    }
public:
    SavingsAccount(int date, int id, double rate);
    int getId() const {return id;}
    double getBalance() const {return balance;}
    double getRate() const {return rate;}
    static double getTotal() {return total;}
    void deposit(int data, double amount);
    void withdraw(int data, double amount);
    void settle(int data);
    void show() const; 
};
View Code

account.cpp

#include "account.h"
#include <cmath>
#include <iostream>
using namespace std;

double SavingsAccount::total=0;
SavingsAccount::SavingsAccount(int date, int id, double rate): id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
    cout << date << "\t#" << id << "is created" << endl;
}
void SavingsAccount::record(int date, double amount) {
    accumulation = accumulate(date);
    lastDate = date;
    amount = floor(amount*100+0.5)/100;
    balance += amount;
    total += amount;
    cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 
}
void SavingsAccount::deposit(int date, double amount) {
    record(date, amount);
}
void SavingsAccount::withdraw(int date, double amount) {
    if(amount>getBalance())
      cout << "Error: not enough money" << endl;
    else
      record(date, -amount);
}
void SavingsAccount::settle(int date) {
    double interest = accumulate(date) * rate/365;
    if(interest != 0)
      record(date, interest);
    accumulation = 0;
}
void SavingsAccount::show() const {
    cout << "#" << id << "\tBalance: " << balance;
}
View Code

5_11.cpp

#include "account.h"
#include <iostream>
using namespace std;

int main() {
    SavingsAccount sa0(1, 21325302, 0.015);
    SavingsAccount sa1(1, 58320212, 0.015);
    
    sa0.deposit(5,5000);
    sa1.deposit(25,10000);
    sa0.deposit(45,5500);
    sa1.withdraw(60,4000);
    
    sa0.settle(90);
    sa1.settle(90);
    
    sa0.show(); cout << endl;
    sa1.show(); cout << endl;
    cout << "Total: " << SavingsAccount::getTotal() << endl;
    return 0;
}
View Code

运行结果

 

标签:const,string,对象,double,void,int,实验,include
From: https://www.cnblogs.com/NFurioso/p/17780872.html

相关文章

  • 实验2 c语言分支与循环基础应用编程
    task11#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;13srand(time(0));//以当前系统时间作为随机种子14for(i=0;i<N;......
  • [AHK2] 向对象原型添加属性和方法
    ahk和js十分相似,其中一点就是可以向本地对象添加自定义方法和属性。下面的脚本向ahk的字符串,数组添加了许多方法,添加之后在使用上就和js更加相似了。;Thisscriptisusedtoextendthemethodsoftheahknativeobjectprototype#RequiresAutoHotkeyv2.0#SingleInstan......
  • 实验2_C语言分枝与循环基础应用编程
    试验任务1task1.c#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=r......
  • 实验二
    实验1代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;13srand(time(0));//以当前系统时间作为随机种子1415......
  • 小白学Python - 使用 Python 的 OpenCV 绘制矩形并提取对象
    使用Python的OpenCV绘制矩形并提取对象OpenCV是一个开源计算机视觉和机器学习软件库。可以在它的帮助下完成各种图像处理操作,例如操纵图像和应用大量滤镜。它广泛用于对象检测、人脸检测和其他图像处理任务。让我们看看如何使用OpenCV在图像上绘制矩形并提取对象。编写代码#......
  • 实验二 类和对象
    task1.cpp方式一t.h#ifndefT_H#defineT_H#include<iostream>#include<string>usingnamespacestd;classT{public:T(intx=0,inty=0);T(constT&t);T(T&&t);~T();voidset_m1(intx);intget_m1()const;......
  • 实验二 类和对象
    实验任务1task1.cpp源码t.h:1#ifndefT_H2#defineT_H34#include<iostream>5#include<string>67usingnamespacestd;89//类T的声明10classT{11public:12T(intx=0,inty=0);//带有默认形值的构造函数13T(constT&t)......
  • 实验2
     ......
  • 实验二
    task1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%(N2......
  • 08_面向对象编程(高级)
    ......