程序一:直角三角形,就斜边长和面积
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;
class Triangle
{
public:
Triangle(double aa,double bb)
{
a=aa;
b=bb;
}
double hypot()
{
return sqrt(a*a+b*b);
}
double area()
{
return 0.5*a*b;
}
private:
double a;
double b;
};
int main()
{ Triangle tri(3,4);
cout << "斜边长:" <<tri.hypot()<<endl;
cout << "面积为:" <<tri.area()<<endl;
return 0;
}
程序二,一个学生类,获取学生信息,设置学生信息,修改学生专业
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;
class Student
{
public:
Student(){;} //定义一个不带参数的默认构造函数
void getInfo()
{
cout<<"学生学号为:\t"<<num<<"姓名:\t"<<name<<"性别:\t"<<sex<<"年龄:\t"<<age<<"专业:\t"<<major<<endl;
}
void setInfo(string snum,string sname,string ssex,int sage,string smajor)
{
this->num=snum;
this->name=sname;
this->sex=ssex;
this->age=sage;
this->major=smajor;
}
bool modifyMajor(string maj)
{
this->major=maj;
return true;
}
private:
string name;
string sex;
int age;
string major;
string num;
};
int main()
{
Student a; //定义一个学生对象,在栈中给它分配存储空间;
Student *p=new Student; //定义一个学生对象,在堆中给它分配存储空间;
p->setInfo("1107","srf","nan",24,"soft engineering");
a.setInfo("1108","dp","nv",23,"soft engineering");
a.getInfo();
a.modifyMajor("computer science");
a.getInfo();
p->getInfo();
return 0;
}
程序三: