#include<iostream>
#include<cstring>
using namespace std;
class student
{
public:
char *name;
string sex;
int num;
int age;
int score1,score2,score3;
int avg;
public:
student(char *name_,string sex_,int num_,int age_,int score1_,int score2_,int score3_)
{
name=new char[strlen(name_)+1];
strcpy(name,name_);
sex=sex_;
num=num_;
age=age_;
score1=score1_;
score2=score2_;
score3=score3_;
}
~student()
{
delete[]name;}
student(const student&stu);
void AVG()
{
avg=(score1+score2+score3)/3;
cout<<"平均分= "<<avg<<endl;
}
void output()
{
cout<<"姓名:"<<name<<" "<<"性别:"<<sex<<' '<<"学号: "<<num<<' '<<"年龄: "<<age<<endl;
cout<<"三科分数:"<<score1<<" "<<score2<<' '<<score3<<endl;
}
};
student::student(const student&stu)
{
this->score1=stu.score1;
this->score2=stu.score2;
this->score3=stu.score3;
}
int main()
{
student stu1("QWE","M",1313,17,90,92,94),avg(stu1);
stu1.output();
avg.AVG();
}
法一:
#include<iostream>
using namespace std;
#include<cmath>
class point
{
double x,y;
public:
point(double xx=0,double yy=0)
{
x=xx;y=yy; }
void setxy(double xx,double yy)
{
x=xx;y=yy; }
void getxy()
{
cout<<x<<" "<<y<<endl; }
double getx(){
return x;}
double gety(){
return y;} };
class line
{
point point1,point2;
double len; double k;
public:
void length(point a,point b)
{
len=sqrt((a.getx()-b.getx())*(a.getx()-b.getx())+ (a.gety()-b.gety())*(a.gety()-b.gety())); }
void getlen() {
cout<<len<<endl; }
void getline(point a,point b)
{
k=(a.gety()-b.gety())*1.0/(a.getx()-b.getx()); cout<<"y-"<<a.gety()<<"=" <<k<<"(x-"<<a.getx()<<")"<<endl; } };
int main()
{
point point11;
point11.getxy();
point point22(4,3);
point22.getxy();
line line1;
line1.length(point11,point22);
line1.getlen();
line1.getline(point11,point22);
}
法二:第一次做的时候长度结果一直是0,后来才发现是因为两个点的值没有传到计算的函数中。
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
private:
int x,y;
public:
int setx(){
return x;}
int sety(){
return y;}
Point(int xx=0,int yy=0);
Point(Point&p);
void ShowPoint();
};
Point ::Point(int xx,int yy)
{
x=xx;y=yy;}
Point::Point(Point&p)
{
x=p.x;
y=p.y;
}
void Point::ShowPoint()
{
cout<<"x= "<<x<<" "<<"y= "<<y<<endl;}
class line
{
private:
Point p1,p2;
double len;
public:
line(Point xp1,Point xp2)
{
Point p1(xp1);Point p2(xp2);}
line(line&l)
{
len=l.len;}
double Getlen(Point&p1,Point &p2)//注意这里
{
len=sqrt((p1.setx()-p2.setx())*(p1.setx()-p2.setx())-(p1.sety()-p2.sety())*(p1.sety()-p2.sety()));
cout<<len<<endl;
}
};
int main()
{
Point p11(2,1),p22(1,1);
p11.ShowPoint();
p22.ShowPoint();
line l(p11,p22);
l.Getlen(p11,p22);
}