定义一个dog类,包含的age,weight等属性,以及对这些属性的操作方法,实现并测试这个类。
1 #include <iostream> 2 using namespace std; 3 class Dog{ 4 private: 5 int age,weight; 6 public: 7 void setdog(int a,int b) 8 { 9 age=a; 10 weight=b; 11 } 12 void show() 13 { 14 cout<<"age is "<<age<<","<<"weight is "<<weight<<"."<<endl; 15 } 16 }; 17 int main() 18 { 19 Dog D; 20 D.setdog(5,15); 21 D.show(); 22 }
设计并测试一个矩形类,其属性为矩形的左下角与右上角两个点的坐标,根据坐标能计算矩形的面积。
1 #include <iostream> 2 using namespace std; 3 class Rectangle{ 4 private: 5 int top,reight,botoom,left; 6 public: 7 Rectangle(int t,int r,int b,int l) 8 { 9 top=t; 10 reight=r; 11 botoom=b; 12 left=l; 13 } 14 ~Rectangle(){ 15 cout<<"over"<<endl; 16 } 17 int area() 18 { 19 int a=top-botoom; 20 int b=reight-left; 21 return a*b; 22 } 23 }; 24 int main() 25 { 26 Rectangle d(100,80,50,20); 27 cout<<"area is: "<<d.area()<<endl; 28 }
标签:weight,int,age,C++,课后,习题,矩形,Rectangle From: https://www.cnblogs.com/Lyh3012648079/p/17347779.html