[C++ Study Notes 4] Experiment 4 - Basic knowledge of classes and objects (1)

[Description]
Declare and implement a Point class, representing a point in the Cartesian coordinate system. The Point class includes:
The private data members x and y of double type represent coordinates.
No-argument (default) constructor, sets the coordinates to the origin.
A parameterized constructor that sets the coordinates to the given parameters.
The accessor functions getX and getY are used to access the x-coordinate and y-coordinate of the point, respectively.
[Input]
0,0 4,5
[Output]
(0,0)
(4,5)
[Source]
Experiment 1 in Chapter 5 of "Programming Basics - Taking C++ as an Example".

#include <iostream>
using namespace std;


/* 请在此处编写Point类 */
class Point//Point类
{
    
    
    private:	//私有的
        double x;//数据成员x和y
        double y;
    public:
        Point(double x=0,double y=0)//有参构造函数,无参时默认x=0,y=0
        {
    
    
           this->x=x;
           this->y=y;
        }
        
        //因为成员变量是private私有的,所以需要通过访问器间接访问
        
        double getX()//访问点的x坐标和y坐标
        {
    
    
            return this->x;
        }
        double getY()
        {
    
    
            return this->y;
        }
    
};


int main()  {
    
    
    double x, y;
    char ignore;
    cin >> x >> ignore >> y;
    Point p1(x, y);
    cin >> x >> ignore >> y;
    Point p2(x, y);
    cout << "(" << p1.getX() << "," << p1.getY() << ")" << endl;
    cout << "(" << p2.getX() << "," << p2.getY() << ")" << endl;
    return  0;
}

[Description]
Declare and implement a Rectangle class to represent a rectangle. The Rectangle class includes:
The private data members width and height of type double, which represent the width and height of the rectangle.
Constructor with default parameters, sets the width and height of the rectangle to the given parameters. The default parameter value for width and height is 1.
The modifier functions setWidth and setHeight are used to modify the width and height of the rectangle respectively.
The accessor functions getWidth and getHeight are used to access the width and height of the rectangle respectively.
The member function computeArea returns the area of ​​the rectangle.
The member function computePerimeter returns the perimeter of the rectangle.
[Input]
5 40
10 3.5
[Output]
200 90
35 27
[Source]
Experiment 2 in Chapter 5 of "Programming Basics - Taking C++ as an Example".

#include <iostream>
using namespace std;


/* 请在此处编写Rectangle类 */
class Rectangle
{
    
    
    private:
        double width;//矩形的宽和高
        double height;
    public:
        Rectangle(double width=1,double height=1)//宽和高的默认参数值为1
        {
    
    
           this->width=width;
           this->height=height;
        }
        
        void setWidth(double width)//修改矩形的宽和高
        {
    
    
            this->width=width;
        }

        void setHeight(double height)
        {
    
    
            this->height=height;
        }
        
        double getWidth()//访问矩形的宽和高
        {
    
    
            return this->width;
        }
        double getHeight()
        {
    
    
            return this->height;
        }

        double computeArea()//返回矩形的面积
        {
    
    
            return this->width*this->height;
        }
        double computePerimeter()//返回矩形的周长
        {
    
    
            return this->width*2+this->height*2;
        }
    
};

int main()  {
    
    
    double width, height;
    cin >> width >> height;
    Rectangle rect1;
    rect1.setWidth(width);
    rect1.setHeight(height);
    cin >> width >> height;
    Rectangle rect2(width, height);
    cout << rect1.computeArea() << " " << rect1.computePerimeter() << endl;
    cout << rect2.computeArea() << " " << rect2.computePerimeter() << endl;
    return 0;
}

[Description]
Declare and implement a Cylinder class to represent a cylinder. The Cylinder class includes:
The private data members radius and height of double type represent the radius and height of the cylinder base respectively.
Constructor with default parameters, sets the base radius and height of the cylinder to the given parameters. The default parameter value for radius and height is 1.
Accessor functions for accessing the base radius and height of the cylinder, respectively.
The member function computeVolume returns the volume of the cylinder.
The member function computeSurfaceArea returns the surface area of ​​the cylinder.
Let's say PI is 3.14159.
【Input】
Input the base radius and height of the cylinder.
[Output]
Output the volume and surface area of ​​the cylinder.
[Input example]
4 8
[Output example]
402.124
301.593
[Source]
Experiment 3 in Chapter 5 of "Programming Basics - Taking C++ as an Example".

#include <iostream>
using namespace std;
const double PI = 3.14159;

/* 请在此处编写Cylinder类 */
class Cylinder
{
    
    
    private:	//私有数据成员radius和height
        double radius;
        double height;
    public:
        Cylinder(double radius=1,double height=1)//带默认参数的构造函数
        {
    
    
           this->radius=radius;
           this->height=height;
        }
        
        
        double computeVolume()//返回圆柱体体积
        {
    
    
            return PI*this->radius*this->radius*this->height;
        }
        double computeSurfaceArea()//返回圆柱体表面积
        {
    
    
            return PI*this->radius*2*this->height+2*PI*this->radius*this->radius;
        }
        
//访问器函数,分别用于访问圆柱体底半径和高,题目要求做,但程序用不到,象征性写一下

        double getRadius()
        {
    
    
            return this->radius;
        }
        double getHeight()
        {
    
    
            return this->height;
        }
    
};


int main() {
    
    
    double radius, height;
    cin >> radius >> height;
    Cylinder cylinder(radius, height);
    cout << cylinder.computeVolume() << endl;
    cout << cylinder.computeSurfaceArea() << endl;
    return 0;
}

[Description]
Declare and implement an Account class that represents a bank account. The Account class includes:
private data member id of string type, indicating account number; private data member name of string type, indicating customer name; private data member balance of double type, indicating account balance; private data member annualInterestRate of double type, indicating annual interest rate .
There is a constructor with parameters, which sets the account number, customer name, account balance, and annual interest rate as given parameters.
The modifier functions setId, setName, setBalance and setAnnualInterestRate are used to modify the account number, customer name, account balance and annual interest rate respectively.
The accessor functions getId, getName, getBalance and getAnnualInterestRate are used to access the account number, customer name, account balance and annual interest rate respectively.
The member function withdraw withdraws money from the account.
The member function deposit deposits money into the account.
The member function computeMonthlyInterestRate returns the monthly interest rate.
The member function print outputs account information.
【Input】
Enter the account number, customer name, account balance and annual interest rate.
【Output】
Output account number, customer name, account balance and monthly interest rate.
【Input example】
112233 ZhangSan 20000 4.5
【Output example】
112233
ZhangSan
20500
0.375%
【Prompt】
Assuming an annual interest rate of 4.5%, use 4.5 as the input value.
Monthly interest rate = annual interest rate/12
[Source]
Experiment 4 in Chapter 5 of "Basics of Programming - Taking C++ as an Example".

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

/* 请在此处编写Account类 */
class Account
{
    
    
    private:
        string id;//账号
        string name;//客户名
        double balance;//账户余额
        double annualInterestRate;//年利率
    public:
        Account(string id,string name,double balance,double annualInterestRate)//构造函数
        {
    
    
           this->id=id;
           this->name=name;
           this->balance=balance;
           this->annualInterestRate=annualInterestRate;
        }
		//访问,修改账号、客户名、账户余额、年利率
		
        void setId(double id)
        {
    
    
            this->id=id;
        }
        string getId()
        {
    
    
            return this->id;
        }

        void setName(double name)
        {
    
    
            this->name=name;
        }
        string getName()
        {
    
    
            return this->name;
        }

        void setBalance(double balance)
        {
    
    
            this->balance=balance;
        }
        double getBalance()
        {
    
    
            return this->balance;
        }

        void setAnnualInterestRate(double annualInterestRate)
        {
    
    
            this->annualInterestRate=annualInterestRate;
        }
        double getAnnualInterestRate()
        {
    
    
            return this->annualInterestRate;
        }

        void withdraw(double n)//从账户中取款
        {
    
    
            this->balance-=n;
        }
        void deposit(double n)//从账户中存款
        {
    
    
            this->balance+=n;
        }
        double computeMonthlyInterestRate(double annualInterestRate)//返回月利率
    	{
    
    
        	return annualInterestRate/12;//月利率
    	}

        void print()//输出账户信息
        {
    
    
            cout<<this->id<<endl<<this->name<<endl<<this->balance<<endl<<this->annualInterestRate/12/100<<"%";
        }
};


int main() {
    
    
    string id;
    string name;
    double balance;
    double annualInterestRate;
    cin >> id >> name >> balance >> annualInterestRate;
    Account account(id, name, balance, annualInterestRate);
    account.withdraw(2500);
    account.deposit(3000);
    account.print();
    return 0;
}

Writing c** is relatively smooth, because I will lose java, if there is any mistake, I hope to point it out, thank you very much, welcome to discuss the topic

Guess you like

Origin blog.csdn.net/qq_49868778/article/details/115215564
Recommended