8月29号

第二题

头文件

#ifndef __class__
#define __class__

#include <iostream>
#include <iomanip>

using namespace std;

class stu
{
private:
    string name;
protected:
    int age;
public:
    double score=100;

    int init(string name,int age,float score);


};

#endif

主函数

#include"class.h"
int main()
{
    string n="江䟠鹅";
    int ag=80;
    double sc=98;

    stu s1;
    s1.init(n,ag,sc);
     while(1)
    {
    cout << "请输入姓名年龄还有分数" << endl;
    cin >> n >> ag >> sc;
    s1.init(n,ag,sc);
    }
    return  0;
}
 

成员函数

#include"class.h"

int stu::init(string name,int age,float score)
{
   this-> name=name;
    stu::age=age;
    this->score=score;
    cout << "name = " << name << endl;
    cout << "age = " << age << endl;
    cout << "score = " << score << endl;
    return 0;
}

第三题

#include <iostream>
#include <iomanip>
 
using namespace std;
 
class Rect
{
private:
    int width;
    int heigth;
public:
 
    void init(int width,int heigth);
    void set_w_h();
    void show();
 
 
};
 
void Rect::init(int width,int heigth)
{
   this->width=width;
   this->heigth=heigth;
}
 
 void Rect::set_w_h()
 {
     int width,heigth;
     cout << "请输入宽度和高度" << endl;
     cin >> width >> heigth ;
     this->width=width;
      this->heigth=heigth;
     this->show();
 }
 
 void Rect::show()
 {
 
     cout << "面积为" << this->width * this ->heigth << endl;
 
 }
 
 
int main()
{
    Rect s1;
    int width,heigth;
     while(1)
    {
    cout << "请输入宽度和高度" << endl;
    cin >> width >> heigth ;
    int pan;
    cout << "请问是否需要修改1,修改 2,不修改" << endl;
    cin >> pan ;
    if(pan==1)
    {
 
        s1.set_w_h();
    }
    else
    {
 
        s1.init(width,heigth);
        s1.show();
    }
}
    return  0;
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值