作业题目:
#include <iostream>
using namespace std;
class Rec
{
const int length;
int width;
public:
void set_length(int l);
void set_width(int w);
int get_length();
int get_width();
void show();
};
#include <iostream>
using namespace std;
class Cir
{
int &r;
public:
void set_r(int r); //设置私有属性r的值
void show(double PI=3.14); //参数有默认值3.14
};
#include <iostream>
using namespace std;
class Car //类名首字母一般大小
{
string color;
string brand;
int speed;
public:
void set(string c,string b,int s);
void display();
void acc(int a);
};
答案:
#include <iostream>
using namespace std;
class Rectangle
{
const int length;
int width;
public:
// void set_length(int len);
Rectangle(int length):length(length){}
void set_width(int wid);
int get_length();
int get_width();
void show();
};
//Rectangle R3;
//void Rectangle::set_length(int len)
//{
// length = len;
//}
void Rectangle::set_width(int width)
{
this->width = width;
}
int Rectangle::get_length()
{
return length;
}
int Rectangle::get_width()
{
return width;
}
void Rectangle::show()
{
cout << this << endl;
int s = 2*(width+length);
int area = width*length;
cout << "周长:" << s << "\t"
<< "面积:" << area << endl;
}
class Circle
{
int &r;
public:
Circle(int &r1):r(r1){}
void set_r(int tr);
void show(float PI = 3.14);
};
void Circle::set_r(int tr)
{
r = tr;
}
void Circle::show(float PI)
{
float s = PI*2*r;
float area = PI*r*r;
cout << "周长:" << s << "\t"
<< "面积:" << area << endl;
}
class Car
{
string color;
string brand;
float speed;
public:
Car():color("red"),brand("玛莎拉蒂"),speed(360){}
void display();
void acc(int a);
void set_color(string c);
void set_brand(string b);
void set_speed(float s);
};
void Car::display()
{
cout << "颜色:" << color << "\t"
<< "品牌:" << brand << "\t"
<< "速度:" << speed
<< endl;
}
void Car::set_color(string c)
{
color = c;
}
void Car::set_brand(string b)
{
brand = b;
}
void Car::set_speed(float s)
{
speed = s;
}
void Car::acc(int a)
{
speed += a;
}
int main()
{
Rectangle rec1(3);
rec1.set_width(3);
cout << "rec1长度:" << rec1.get_length() << "\t"
<< "rec1宽度:" << rec1.get_width() <<endl;
rec1.show();
int r = 1;
Circle r1(r);
r1.show();
r1.set_r(2);
r1.show();
Car car1;
car1.display();
car1.set_color("red");
car1.set_brand("奥迪");
car1.set_speed(240);
car1.display();
car1.acc(5);
car1.display();
return 0;
}
自己定义构造函数:
#include <iostream>
#include <cstdlib>
using namespace std;
class Stu
{
int age;
double score;
public:
//Stu(){cout << "Stu的无参构造" << endl;}
//实现Stu的有参构造
Stu(int age,double score)
{
this->age = age;
this->score = score;
cout << "Stu的有参构造" << endl;
}
void show();
};
void Stu::show()
{
cout << "age = " << age << endl;
cout << "score = " << score << endl;
}
int main()
{
//Stu s1;
//Stu *p1 = (Stu*)malloc(sizeof(Stu));
Stu *p1;
cout << "*********" << endl;
p1 = new Stu(1,3.14); // new Stu{1,3.14}也可以
//类中只提供了有参构造,所以new申请空间时必须给初始值可以用{},也可以用()
p1->show();
//Stu s1(12,31.9); //调用了有参构造
return 0;
}
参数和成员属性同名:
#include <iostream>
#include <cstdlib>
using namespace std;
class Stu
{
int age;
double score;
public:
Stu(int age,double score):age(age),score(score)
{cout << "Stu有两个参数的有参构造" << endl;}
void show();
};
void Stu::show()
{
cout << "age = " << age << endl;
cout << "score = " << score << endl;
}
int main()
{
Stu s2(12,31.9);
s2.show();
return 0;
}
类中存在引用成员时:
#include <iostream>
#include <cstdlib>
using namespace std;
class Stu
{
int &age;
double score;
public:
Stu(int &age,double score):age(age),score(score)
{cout << "Stu有两个参数的有参构造" << endl;}
void show();
void set_age()
{age=0;}
};
void Stu::show()
{
cout << "age = " << age << endl;
cout << "score = " << score << endl;
}
int main()
{
int age = 18;
Stu s2(age,31.9);
s2.show();
s2.set_age();
cout << age << endl; //局部变量age的值为0,因为类对象中的age引用了该局部变量
return 0;
}
类中存在const修饰的成员时:
#include <iostream>
#include <cstdlib>
using namespace std;
class Stu
{
const int age;
double score;
public:
//Stu():age(23){cout << "Stu的无参构造" << endl;}
//实现Stu的有参构造
Stu(int age,double score):age(age),score(score)
{cout << "Stu有两个参数的有参构造" << endl;}
Stu(int age):age(age)
{cout << "Stu有一个参数age的有参构造" << endl;}
/*Stu(double score):age(20)
{
this->score = score;
cout << "Stu有一个参数score的有参构造" << endl;
}*/
void show();
};
void Stu::show()
{
cout << "age = " << age << endl;
cout << "score = " << score << endl;
}
int main()
{
int age = 18;
Stu s2(age,31.9);
s2.show();
Stu s1(24);
s1.show();
return 0;
}
类中存在其他类对象时:
#include <iostream>
using namespace std;
class Stu
{
int age;
float score;
public:
Stu(int age,float score):age(age),score(score){}
int get_age()
{
return age;
}
double get_score()
{
return score;
}
};
class Person
{
const int id;
const string name;
Stu stu1;
public:
Person(int id,string name,Stu stu1):id(id),name(name),stu1(stu1){}
Person(int id,string name,int age,float score):id(id),name(name),stu1(age,score){}
void show()
{
cout << "id = " << id << "\t"
<< "name = " << name << "\t"
<< "age = " << stu1.get_age() << "\t"
<< "score = " << stu1.get_score() << endl;
}
};
int main()
{
Stu stu1(18,99.9);
Person p1(10086,"张三",stu1);
p1.show();
Person p2(10085,"李四",19,88.8);
p2.show();
return 0;
}