3.26OOP实验

本文详细介绍了C++中的类定义与对象实例,包括学生、存折、几何形状和身体评估类,通过实际代码展示了如何创建和操作这些类。
摘要由CSDN通过智能技术生成

A. 对象数组(类和对象)

#include <bits/stdc++.h>
using namespace std;
class Student
{
    public : 
    void input(string name , string number , string sex , string college , string PhoneNuber);
    void Print()
    {
        cout << name << endl;
    }
    private :
    string name , number , sex , college , PhoneNumber;
};
void Student::input(string name1 , string number1 , string sex1 , string colloege1 , string PhoneNumber1)
{
    name = name1 , number = number1 , sex = sex1 , college = colloege1 , PhoneNumber = PhoneNumber1;
}
int main()
{
    Student student[20];
    int n ;
    cin >> n ;
    for(int i = 0 ; i < n ; i ++)
    {
        string name , number , sex , college , PhoneNumber;
        cin >> name >> number >> sex >> college >> PhoneNumber;
        student[i].input(name , number , sex , college , PhoneNumber);
    }
    for(int i = 0 ; i < n ; i ++)
    {
        student[i].Print();
    }
    return 0;
}

B. 存折类定义(类与对象)

#include <bits/stdc++.h>
using namespace std;
class CAccount
{
	public:
	void deposit(float money);
	void withdraw(float money);
	void input(string account1 , string name1 , float balance1);
	void check();
	private:
	string account , name ;
	float balance ;
};
void CAccount::check()
{
    cout << account;
    cout << "'s balance is" << " ";
	cout << balance << endl;
}
void CAccount::input(string account1 , string name1 , float balance1)
{
	account = account1 , name = name1 , balance = balance1;
}
void CAccount::deposit(float money)
{
	balance += money;
	cout << "saving ok!" << endl;
} 
void CAccount::withdraw(float money)
{
	if(money > balance)
	{
		cout << "sorry! over limit!" << endl;
		return ;
	}
	else
	{
		balance -= money;
		cout << "withdraw ok!" << endl;
	}
}
int main()
{
	CAccount a[10010];
	int n= 2;
	for(int i = 0 ; i < n ; i ++)
	{
		string name , account ;
		float balance ;
		cin >> name >> account ;
		cin >> balance ;
		a[i].input(account,name,balance);
		float deposit_money , withdraw_money;
		
        cin >> deposit_money >> withdraw_money;
		a[i].check();
        a[i].deposit(deposit_money);
		a[i].check();
		a[i].withdraw(withdraw_money);
		a[i].check();
	}
	return 0 ;
}

C. 点和圆 (类与对象)

#include <bits/stdc++.h>
using namespace std;
//----类定义----
class Point
{
    public:
    void setCenter(float xx ,float yy);
    void setRadius(float rr);
    void getxy(float fxx ,float fyy);
    float getArea() ;
    float getLength() ;
    bool contain();
    private:
    float x , y ,r;
    float fx , fy;
};
//----类实现----
void Point::setCenter(float xx ,float yy){x = xx , y = yy;}
void Point:: setRadius(float rr){r=rr;}
void Point::getxy(float fxx ,float fyy){fx = fxx , fy = fyy;}
float Point::getArea() {return 3.14*r*r;}
float Point::getLength() {return 2*r*3.14;}
bool Point::contain()
{
    if(pow((pow(x-fx,2)+pow(y-fy,2)),0.5) <= r) return true;
    else return false;
}

//----主函数----
int main()
{
    float x , y , r ;
    cin >> x >> y >> r ;
    float fx , fy ;
    cin >> fx >> fy;
    Point p;
    p.setCenter(x,y);
    p.setRadius(r);
    p.getxy(fx,fy);
    float Area = p.getArea();
    float Length = p.getLength();
    printf("%.2f %.2f\n",Area,Length);
    if(p.contain())
    {
        printf("yes\n");
    }
    else printf("no\n");
    return 0;
}

D. 最胖的加菲(类与对象+数组)

#include <bits/stdc++.h>
using namespace std; 
class Cat
{
    private:
    string name;
    double weight;
    public:
        Cat(){ }
        void set(string n,double w)
        {
           name=n;
           weight=w;
        }
        string getN(){return name;}
        double getW(){return weight;}
 
};
bool comp(Cat c1, Cat c2)
{
    return c1.getW()<c2.getW();
}
 
int main()
{
    int t;
    cin >> t;
    string name;
    float weight;
    Cat *c=new Cat[t];
 
    for(int i=0;i<t;i++){
        cin>>name>>weight;
        c[i].set(name,weight);
    }
 
    sort(c,c+t,comp);
 
    for(int i=0;i<t-1;i++)
        cout<<c[i].getN()<<" ";
    cout<<c[t-1].getN()<<endl;
    delete[] c;
    return 0;
}

E. 身体评估(类与对象)

#include <bits/stdc++.h>
using namespace std;
class num
{
    public:
    float BMI();
    float tiz();
    string get(string name1)
    {
        return name1;
    }
    void input(string name1 , float high1 , float weight1 , float waist1)
    {
        name = name1 , high = high1 , weight = weight1 , waist = waist1;
    }
    private:
    string name;
    float high , weight , waist;
};
float num:: BMI()
{
    return weight/pow(high,2);
}
float num :: tiz()
{
    float a = waist * 0.74;
    float b = weight * 0.082 + 34.89;
    //printf("%f %f ",b,a);
    return abs((a-b))/weight;
}
int main()
{
    int t;
    cin >> t;
    num a[10010];
    for(int i = 0 ; i < t ; i ++)
    {
        string name1 ;
        float high1 , weight1 , waist1;
        cin >> name1 ;
        cin >> high1 >> weight1 >> waist1 ;
        a[i].input(name1 , high1 , weight1 , waist1);
        float BMI = a[i].BMI();
        float tiz = a[i].tiz();
        for(int i = 0 ; i < name1.length() ; i++)
        {
            printf("%c",name1[i]);
        }
        printf("的BMI指数为%.0f--体脂率为%.2f\n",round(BMI),tiz);

    }
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值