第二题
头文件
#ifndef __class__
#define __class__
#include
#include
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
#include
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;
}