C++题目练习05
5.1 基类指针指向派生类对象:虚函数的使用
设计基类 Point,数据成员有:坐标值x 和 坐标值y,成员函数有:构造函数,面积函数Area();
Point的派生类 Rectangle ,增加数据成员:坐标值x1 和 坐标值y1,同样有构造函数 和 面积函数Area();
Point的派生类 Circle,增加数据成员:半径r,同样有构造函数 和 面积函数Area();
#include <iostream>
using namespace std;
class Point {
protected:
double x, y;
public:
Point(double a, double b) {
x = a;
y = b;
}
virtual double Area() {
cout << "Call Point's Area function." << endl;
return x * y;
}
};
class Rectangle: public Point {
protected:
double x1, x2;
public:
Rectangle(double a, double b, double c, double d)
: Point(a, b) {
x1 = c;
x2 = d;
}
double Area() {
cout << "Call Rectangle's Area function." << endl;
return (x1 - x) * (x2 - y);
}
};
class Circle: public Point {
protected:
double R;
public:
Circle(double a, double b, double r)
: Point(a, b) {
R = r;
}
double Area() {
cout << "Call Circle's Area function." << endl;
return 3.14 * R * R;
}
};
double CalcArea( Point &ref ) {
return ref.Area( );
}
int main( ) {
Point p(0, 0);
Rectangle r(1, 2, 4, 5);
Circle c( 0, 0, 100 );
cout << CalcArea( p ) << endl;
cout << CalcArea( r ) << endl;
cout << CalcArea( c ) << endl;
Point *q = &c;
cout << q->Area() << endl;
q = &r;
cout << q->Area() << endl;
return 0;
}
5.2 设计矩形类rectangle
定义并实现一个矩形类 rectangle,有长(length)、宽(wide) 两个属性,
成员函数 area() 计算矩形的面积,成员函数 setlength()、getlength()、setwide()、getwide() 分别设置和获取 length 或者 wide 的值,
成员函数 display() 输出矩形的信息(长,宽,面积),
要求定义构造函数、拷贝构造函数、赋值运算符函数,能使用对象数组。
#include <iostream>
using namespace std;
class rectangle {
private:
double length = 0, wide = 0;
public:
rectangle(double l, double w) {
length = l;
wide = w;
}
rectangle() {
}
rectangle(rectangle &c) {
length = c.length;
wide = c.wide;
}
double area() {
return length * wide;
}
void setlength(double l) {
length = l;
}
double getlength() {
return length;
}
void setwide(double w) {
wide = w;
}
double getwide() {
return wide;
}
void display() {
cout << "message of the rectangle:length=" << length << " wide=" << wide << " area=" << area() << endl;
}
};
int main( ) {
rectangle r1( 3, 4 ); // 定义一个矩形 r1,长为 3,宽为 4
r1.display(); // 输出矩形r1的有关信息
rectangle r2; // 定义一个矩形r2
r2 = r1;
r2.display(); // 输出矩形 r2 的有关信息
r2.setlength( 10 ); // 把矩形 r2 的长 length 改为 10
r2.setwide( 20 ); // 把矩形 r2 的宽 wide 改为 20
r2.display(); // 再输出矩形 r2 的有关信息
rectangle r3( r1 );
r3.display(); // 输出矩形 r3 的有关信息
rectangle r4[4]; // 定义矩形数组 r4
for ( int i = 0; i < 4; i ++ ) // 输出矩形数组 r4 中各个矩形的信息
r4[i].display();
return 0;
}
5.3 字符串类的实现 2
在《字符串类的实现1》的基础上(指路C++题目练习02的2.3)
增加 myString字符串类 的方法:
-
size(),返回当前对象含字符的个数,如果不含字符串,返回0
-
int set( char s[] ),逐字符拷贝 s[] 到 data[],len 也要相应改变。返回值是新的字符个数,即 len
-
重载 赋值运算符 myString & operator= ( const myString & b ) :将右侧运算对象 myString对象赋值给 左侧运算对象 myString对象。
注意:1. 如果左侧运算对象的data非空,要先释放;2. 要实现深拷贝。 关于深拷贝,可参考最下方的附录 -
重载 +运算符 myString & operator+ ( const myString & b ) :把右侧运算对象 b 的 data 追加到左侧运算对象 a 的 data 后面,返回一个新对象的引用
-
取子串函数 sub( int start, int l ):返回一个 myString 对象,其 data 为当前对象的 data[start, start + l]。注意:当开始位置 start 大于当前对象的字符个数时,返回一个 data 为空的对象;当 start + l 大于当前对象字符总数时,取到末尾
#include <iostream>
#include <cstring>
using namespace std;
class myString
{
private:
char *data;
int len;
public:
myString()
{
data = nullptr;
len = 0;
}
myString(char s[])
{
len = strlen(s);
data = new char[len + 1];
strcpy(data, s);
}
~myString()
{
if (data != nullptr)
{
delete[] data;
}
}
myString(const myString &b)
{
len = b.len;
data = new char[len + 1];
strcpy(data, b.data);
}
int size()
{
return len;
}
int set(char s[])
{
if (data != nullptr)
{
delete[] data;
}
len = strlen(s);
data = new char[len + 1];
strcpy(data, s);
return len;
}
myString &operator=(const myString &b)
{
if (this == &b)
{
return *this;
}
if (data != nullptr)
{
delete[] data;
}
len = b.len;
data = new char[len + 1];
strcpy(data, b.data);
return *this;
}
myString operator+(const myString &b)
{
myString temp;
temp.len = len + b.len;
temp.data = new char[temp.len + 1];
strcpy(temp.data, data);
strcat(temp.data, b.data);
return temp;
}
myString sub( int start, int l )
{
myString ms;
ms.data=new char[l+1];
ms.len=l;
for(int i=0;i<l;i++)
ms.data[i]='\0';
int j=0;
for(int i=start-1;(i<start+l-1)&&(i<len);i++,j++){
ms.data[j]=data[i];
}
return ms;
}
void output()
{
if (data != nullptr)
{
cout << data;
}
}
};
int main()
{
myString tmp;
cout << tmp.size() << endl; // 预期输出:0
char s1[] = "abcd";
myString a( s1 );
a.output(); // 预期输出:abcd
cout << endl;
cout << a.size() << endl; // 预期输出:4
char s2[] = "1234";
myString b( s2 );
b.output(); // 预期输出:1234
cout << endl;
myString c;
c = a; // 赋值运算符
c.output(); // 预期输出:abcd
cout << endl;
cout << c.size() << endl; // 预期输出:4
c = a + b;
c.output(); // 预期输出:abcd1234
cout << endl;
cout << c.size() << endl; // 预期输出:8
b = c.sub( 3, 4 );
b.output(); // 预期输出:cd12
cout << endl;
b = c.sub( 6, 10 ); // 从第6个位置至末尾,最多只有3个字符,全部取完
b.output(); // 预期输出:234
cout << endl;
b = c.sub( 9, 2 ); // 第9个位置已经超越c的长度,取得空字符串
b.output(); // 预期没有字符输出
cout << endl;
char s3[] = "77777777777777777777";
c.set( s3 );
c.output(); // 预期输出20个7
cout << endl;
return 0;
}