20176510遥感一班李长宁

@李长宁的笔记 类
1.定义一个Book(图书)类,在该类定义中包括以下数据成员和成员函数。
数据成员: bookname(书名)、price(价格)和number(存书数量)。
成员函数: display()显示图书的情况;borrow()将存书数量减1,并显示当前存书数量;restore()将存书数量加1,并显示当前存书数量。
在main函数中,要求创建某一种图书对象,并对该图书进行简单的显示、借阅和归还管理。
#include
#include
using namespace std;
class book
{
public:
book(string book1,float price1,int number1);
void dispaly();

book::book(string bookname1,float price1,int number1)
{
bookname=bookname1;
price=price1;
number=number1;
}
void book::dispaly()
{
cout<<“bookname:”<<bookname<<endl;
cout<<“price:”<<price<<endl;
cout<<“number:”<<number<<endl;
}
void book::borrow()
{
number-=1;
cout<<“number:”<<number;
}
void book::restore()
{
number+=1;
cout<<“number:”<<number;
}
int main()
{
book a(0,0,0);
a.dispaly();
a.borrow();
a.restore();

return 0;

}
void borrow();
void restore();
private:
string bookname;float price;int number;
};
2.定义一个Box(盒子)类,在该类定义中包括以下数据成员和成员函数。
数据成员:length(长)、width(宽)和height(高)。
成员函数:构造函数Box,设置盒子的长、宽和高3个初始数据;成员函数setBox对数据成员置值;成员函数volume 计算盒子的体积。
在main函数中,要求创建Box对象,输入长、宽、高,输出盒子的体积。
#include
using namespace std;
class Box
{public:
Box(double l,double w,double h);
void Setbox(double l,double w,double h);
void Valume(double l,double w,double h) ;
private:
double length;double width;double height;};
Box::Box(double l,double w,double h)
{length=l;
width=w;
height=h;}
void Box::Setbox(double l,double w,double h){
length=l;width=w;height=h;
}
void Box::Valume(double l,double w,double h){
double v;
v=lwh;
cout<<“体积是”<<v;
}
int main(){
Box bb(0,0,0);
double a,b,c;
cin>>a>>b>>c;
bb.Setbox(a,b,c);
bb.Valume(a,b,c);
}
3.定义一个Student类,在该类定义中包括:
一个数据成员(分数score)及两个静态数据成员(总分total和学生人数count);
成员函数scoretotalcount(double s) 用于设置分数、求总分和累计学生人数;静态成员函数sum()用于返回总分;静态成员函数average()用于求平均值。
在main函数中,输入某班同学的成绩,并调用上述函数求全班学生的总分和平均分。
.#include
using namespace std;

class Student{
private:
double score;
static int count;
static double total;
public:
void scoretotalcount(double s)
{score=s;
++count;
total=total+score;
}

static double sum()
{return total;
}

static double average()
{
return total/count;
}

};

int Student:: count=0;
double Student:: total=0.0;

int main()
{
Student s1,s2;
s1.scoretotalcount(99);
cout<<Student::sum()<<endl;
cout<<Student::average()<<endl;
s2.scoretotalcount(70);
cout<<Student::sum()<<endl;
cout<<Student::average()<<endl;
return 0;

}
4.定义一个表示点的结构类型Point和一个由直线方程y = ax + b确定的直线类Line。
结构类型Point有两个成员x和y,分别表示点的横坐标和纵坐标。
Line类有两个数据成员a和b,分别表示直线方程中的系数。
Line类有一个成员函数print用于显示直线方程。友元函数setPoint(Line &l1,Line &l2)用于求两条直线的交点。
在main函数中,建立两个直线对象,分别调用print函数显示两条直线的方程,并调用函数setPoint求这两条直线的交点。
#include

using namespace std;

struct Point
{
double x;
double y;
};
class Line
{
friend Point setPoint(Line &l1,Line &l2);
public:
void print(double a0,double b0);
private:
double a;
double b;
};
void Line::print(double a0,double b0)
{
a=a0;
b=b0;
cout<<“y=”<<a<<“x+”<<b;
}
Point setPoint(Line &l1,Line &l2)
{
Point p;
p.x=(l2.b-l1.b)/(l1.a-l2.a);
p.y=l1.a*p.x+l1.b;
return p;
}

int main()
{
Line m,n;
Point q;
double a1,b1,a2,b2;
cin>>a1>>b1>>a2>>b2;
m.print(a1,b1);
n.print(a2,b2);
q=setPoint(m,n);
cout<<q.x<<" "<<q.y;
return 0;
}
5.用类成员结构修改第4小题的程序,使其实现相同的功能。
定义Point类和Line类,表示点和线;
定义setPoint类,包含两个Line类成员和一个表示直线交点的Point成员,并定义类中求直线交点的成员函数。编写每个类相应的成员函数和测试用的主函数。
#include

using namespace std;

class Line
{
friend void setPoint(Line &l1,Line &l2);
public:
void print(double a0,double b0);
private:
double a;
double b;
};
void Line::print(double a0,double b0)
{
a=a0;
b=b0;
cout<<“y=”<<a<<“x+”<<b;
}

class Point
{
friend void setPoint(Line &l1,Line &l2);
private:
void sr(double x0,double y0);
double x;
double y;
};
void Point::sr(double x0,double y0)
{
x=x0;y=y0;
cout<<x<<" "<<y;
}
void setPoint(Line &l1,Line &l2)
{
double x1,y1;
Point p;
x1=(l2.b-l1.b)/(l1.a-l2.a);
y1=l1.a*x1+l1.b;
p.sr(x1,y1);
}

int main()
{
Line m,n;
//static Point p;
double a1,b1,a2,b2;
cin>>a1>>b1>>a2>>b2;
m.print(a1,b1);
n.print(a2,b2);
setPoint(m,n);
return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值