首页 > 编程语言 >实验2 类和对象_基础编程2

实验2 类和对象_基础编程2

时间:2023-10-22 19:35:11浏览次数:32  
标签:const 对象 double void 编程 int Complex 实验 include

1.实验任务3

程序代码组织如下:

•Complex.h 类Complex的声明

#pragma once

# include<iostream> 
# include<cmath>

class Complex{
public:
    Complex(double r = 0, double i = 0);
    Complex(const Complex &c);
    ~Complex(){};
    
    double get_real() const;
    double get_imag() const;
    
    void add(const Complex &c);
    void show() const;
    
    friend Complex addl(const Complex &c1 , const Complex &c2);
    friend bool is_equal(const Complex &c1 , const Complex &c2);
    friend double abs(const Complex &c1);
    
private:
    double real;
    double imag;
};

 

•Complex.cpp 类Complex的实现

# include"Complex.h"

Complex::Complex(double r,double i):real{r},imag{i}{}
Complex::Complex(const Complex &c):real{c.real},imag{c.imag}{}

double Complex::get_real() const {return real;}
double Complex::get_imag() const {return imag;}

void Complex::add(const Complex &c){
    real += c.real;
    imag += c.imag;
}

void Complex::show() const {
    if(imag>0)
   {std::cout << real << " + " << imag << "i"; }
   else if(imag<0)
   {std::cout << real << imag << "i"; }
   else if(imag ==0)
   {std::cout << real ; }  
}

Complex addl(const Complex &c1 , const Complex &c2){
    double resultreal = c1.real + c2.real;
    double resultimag = c1.imag + c2.imag;
    return Complex (resultreal,resultimag);
}

bool is_equal(const Complex &c1 , const Complex &c2){
    return (c1.real == c2.real)&&(c1.imag == c2.imag);
}

double abs(const Complex &c){
    double x = sqrt(c.real*c.real + c.imag*c.imag);
    return x;
    }

 

•main.cpp 测试代码、主函数

# include"Complex.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 = addl(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.实验任务4

程序组织代码如下:

•User.hpp 文件源码

# pragma once

# include<iostream>
# include<string>
# include<iomanip>
using namespace std;
class User{
public:
    User();
    User(string name0, string passwd0 = "111111", string email0 = " ");

    void set_email();
    void change_passwd();
    string get_name() const;
    string get_passwd() const;
    string get_email() const;
    void print_info();
    
public:
    static void print_n();
    
private:
    string name, passwd, email;
    
private:
    static int n;
};

int User::n = 0;

User::User(){ ++n;}
User::User(string name0, string passwd0, string email0):name{name0}, passwd{passwd0}, email{email0}{ ++n;}

string User::get_name() const {
    return name;
};

string User::get_email() const {
    return email;
};

string User::get_passwd() const {
    return passwd;
};

void User::set_email() {
    cout << "Enter email address: " ;
    cin >> email;
    cout <<"email is set successfully......" << endl;
}

void User::change_passwd(){
    string passwd1;
    int i = 0;
    cout << "Enter old password: ";
    cin >> passwd1;
    while(passwd1 != "111111" && i < 2 )
    { i++;
     cout << "password input error. Please re-enter again: ";
     cin >> passwd1;
    }
    if(passwd1 == "111111"){
        cout << "Enter new passwd: ";
        string reset_passwd;
        cin >> reset_passwd;
        passwd = reset_passwd;
        cout << "new passwd is set successfully..." << endl ;
    }
    else if( i = 2 )
    cout << "password input error. Please try after a while." << endl;        
    }

void User::print_info(){
    string s2(passwd.size(),'*');
    cout << "name: " << name << endl;
    cout << "password: " << s2 << endl;
    cout << "email: " << email <<endl;
}

void User::print_n(){
    cout << "there are " << n << " users." << endl;
}

 

•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 << "testing2......\n";
    
    User user2("Leonard");
    user2.print_info();
    user2.change_passwd();
    user2.set_email();
    user2.print_info();
    cout << endl;
    User::print_n();    
}

int main(){
    test();
}

 

运行结果测试截图1:

 

运行结果测试截图2:

 

 

 

3.实验任务5

程序组织代码如下:

•account.h 类account的声明

# ifndef ACCOUNT_H__
# define 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 date, double amount);
    void withdraw(int date, double amount);
    void settle(int date);
    void show() const;
};
# endif

 

•account.cpp 类account的实现

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

 

•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;
}

 

运行结果测试截图:

 

 

实验备注:为了使输出界面使给出的一致,这里在编写代码的时候对测试函数进行了一些类似于格式控制的修改。

标签:const,对象,double,void,编程,int,Complex,实验,include
From: https://www.cnblogs.com/Rainbow-forest/p/17780812.html

相关文章

  • 实验2 类与对象
     3.实验任务31#pragmaonce23#include<iostream>4#include<cmath>56classComplex{7public:8Complex(doubler=0,doublei=0):real(r),imag(i){}910doubleget_real()const{11returnreal;12}......
  • 实验2 类与对象
    实验一方式一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;intge......
  • 实验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......
  • 高性能处理器架构与编程(一)
    引言:云-端-边协同:同构协同(ARM、具体实例为鲲鹏)同操作系统(Vela,在物联网侧的统一)课时安排鲲鹏920和ARM处理器架构介绍(第一周)鲲鹏处理器基础实验(10课时)(第二、三周)鲲鹏处理器系统实验(8课时)(第四、五周)应用编程实验(8课时)上午华为海思的产品系列通用计算处理器:鲲鹏手机应用处......
  • 实验二
    实验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)......