C++ day3
#include <iostream>
using namespace std;
class rec
{
const int length;
int width;
public:
rec (int length):length(length){};
void set_width(int W);
int get_length();
int get_width();
void show();
};
void rec::set_width(int W)
{
width=W;
}
int rec::get_length()
{
return length;
}
int rec::get_width()
{
return width;
}
void rec::show()
{
int l,s;
l=2*(length+width);
s=length*width;
cout<<"周长:"<<l<<endl;
cout<<"面积:"<<s<<endl;
}
int main()
{
rec s(6);
s.set_width(5);
int a=s.get_width();
int b=s.get_length();
cout<<a<<endl;
cout<<b<<endl;
s.show();
return 0;
}
#include <iostream>
using namespace std;
class y
{
int &R;
public:
y(int &R):R(R){};
void show();
};
void y::show()
{
double PI=3.14;
double l,s;
l=2*PI*R;
s=PI*R*R;
cout<<"周长:"<<l<<endl;
cout<<"面积:"<<s<<endl;
}
int main()
{
int r=4;
y s(r);
s.show();
return 0;
}
#include <iostream>
using namespace std;
class car
{
string se;
string brand;
int speed;
public:
car(string a,string b,int c):se(a),brand(b),speed(c){};
void display();
void acc(int a);
};
void car::display()
{
cout<<se<<endl;
cout<<brand<<endl;
cout<<speed<<endl;
}
void car::acc(int a)
{
speed=a;
}
int main()
{
string a,b;
getline(cin,a);
getline(cin,b);
car s(a,b,50);
s.display();
s.acc(80);
return 0;
}
原文地址:https://blog.csdn.net/2302_81211062/article/details/144383128
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!