HZAU CPlusPlus 类的基础

1.设计并测试名为Student和ArrayOfStudents的类。

Student类说明如下:

(1)有3个私有数据成员name,id,grade,描述属性值

(2)声明3个成员函数,其一默认构造函数,为各属性值设定初始值;其二setValues完成三个属性值的初始化;其三output完成输出。

ArrayOfStudents类说明如下:

(1)声明2个私有数据成员,其一为Student类型的指针;其二为数组长度。

(2)设计1个成员函数element要求能够取出第i个对象,并对其属性值进行修改。

(3)设计构造函数和析构函数,并在构造函数中new空间,析构函数中delete空间,并cout输出提示语句。

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
class Student
{
private:
	char* name, * id;
	int grade;
public:
	Student()
	{
		char s[4] = { "***" };
		char d[4] = { "000" };
		name = new char(4);
		id = new char(4);
		strcpy(name, s);
		strcpy(id, d);
		grade = 0;
	}

	void setValues(char* i, char* j, int k)
	{
		int a = strlen(i);
		delete(name);
		name = new char[a];
		strcpy(name, i);
		int b = strlen(j);
		delete(id);
		id = new char[b];
		strcpy(id, j);
		grade = k;
	}

	void output()
	{
		printf("Name:%s id:%s grade:%d\n", name, id, grade);
	}
};

//void a1()
//{
//	Student p[2];
//	p[0].output();
//	p[1].output();
//	p[0].setValues("aaa","001",88);
//	p[1].setValues("bbb","002",90);
//	p[0].output();
//	p[1].output();
//}

class ArrayOfStudents
{
private:
	int num;
	Student* p;
public:
	ArrayOfStudents(int count)
	{
		num = count;
		p = new Student[num];
	}

	~ArrayOfStudents()
	{
		delete(p);
		p = NULL;
		printf("Deleting...");
	}

	Student& element(int i)
	{
		return p[i];
	}
};

int  main() 
{
	//	a1();
	int count;
	scanf("%d", &count);   //请输入学生人数;
	ArrayOfStudents stus(count);   //创建数组对象     
	stus.element(0).output();
	stus.element(1).output();
	stus.element(0).setValues("ZhangSan", "001", 85);
	stus.element(1).setValues("Lisi", "002", 90);
	stus.element(0).output();
	stus.element(1).output();
	return 0;

}

2.(1)在前一周作业题(椭圆类——2)的基础上,增加一个Point类(点类),包括私有成员横纵坐标x和y(均为int型),以及参数带默认值的构造函数Point(x=0,y=0);

(2)给Point类增加拷贝(复制)构造函数Point(Point& p);

(3)增加析构函数~Point();输出"Point xigou"以及待析构的点的横纵坐标。

(4)增加取横纵坐标值的函数int GetX(); int GetY();

(5)删除Ellipse类原来的四个数据成员,重新修改为 Point a,b;

(6)修改Ellipse原来的构造函数Ellipse(int x1,int y1,int x2,int y2);注意使用冒号语法对组合成员进行构造,并在构造函数里输出“gouzao 1”和x1,y1,x2,y2的值。

(7)增加一个Ellipse类的构造函数Ellipse(Point &p1,Point &p2); 并在构造函数里输出“gouzao 2”和x1,y1,x2,y2的值。

(8)修改Ellipse的析构函数的输出内容为“Ellipse xigou”以及待析构对象的外切矩形右下角的横纵坐标。

(9)去掉Ellipse类的拷贝构造函数、Fun函数,并修改剩余函数里的错误。

#include<iostream>
#include<string.h>
#include<stdio.h>
#define pai 3.1415;
using namespace std;


class Point
{
private:
	int x, y;
public:
	Point(int xx = 0, int yy = 0) :x(xx), y(yy)
	{

	}

	Point(Point& p)
	{
		x = p.x;
		y = p.y;
	}

	~Point()
	{
		printf("Point xigou %d %d\n", x, y);
	}

	int GetX()
	{
		return x;
	}

	int GetY()
	{
		return y;
	}


};

class Ellipse
{
private:
	Point a, b;
public:
	Ellipse(int xx1, int yy1, int xx2, int yy2) :a(xx1, yy1), b(xx2, yy2)
	{
		printf("gouzao 1 %d %d %d %d\n", xx1, yy1, xx2, yy2);
	}

	Ellipse(Point& p1, Point& p2) :a(p1), b(p2)
	{
		printf("gouzao 2 %d %d %d %d\n", p1.GetX(), p1.GetY(), p2.GetX(), p2.GetY());
	}


	~Ellipse()
	{
		printf("Ellipse xigou %d %d\n", b.GetX(), b.GetY());
	}

	void Area()
	{
		double area;
		area = (double)b.GetX() * a.GetY() * pai;
		if (a.GetY() == 1) {
			printf("%.4lf\n", area);
		}
		else
			printf("%.3lf\n", area);
	}

	void Show()
	{
		printf("%d %d %d %d\n", a.GetX(), a.GetY(), b.GetX(), b.GetY());
	}
};

int main(void)
{
	int a, b, c, d;
	scanf("%d %d %d %d", &a, &b, &c, &d);
	int a1, b1, c1, d1;
	scanf("%d %d %d %d", &a1, &b1, &c1, &d1);
	Ellipse e1(a, b, c, d);
	Point p1(a1, b1), p2(c1, d1);
	Ellipse e2(p1, p2);
	e1.Show();
	e2.Show();
	e1.Area();
	e2.Area();
	return 0;
}

​

3.定义一个坐标点类Point和求两点距离的距离类Distance,在每个类的构造函数函数体里加上cout输出相应的提示语句,以便观察构造函数被调用的顺序。 

#include<iostream>
#include<math.h>
using namespace std;
class Point
{
public:
	Point(int xx, int yy);
	Point(Point& r);
	int GetX();
	int GetY();
	~Point();
private:
	int x, y;
};

Point::Point(int xx, int yy)
{
	x = xx;
	y = yy;
	cout << "Point's constructor was called" << endl;
}
Point::Point(Point& r)
{
	x = r.x;
	y = r.y;
	cout << "Point's copyConstructor was called" << endl;
}
int Point::GetX()
{
	return x;
}
int Point::GetY()
{
	return y;
}
Point::~Point()
{
	cout << "Point's destructor was called" << endl;
}
class Distance
{
private:
	Point p1, p2;
	double dist;
public:
	Distance(Point a, Point b);
	double GetDis();
	~Distance();
};
Distance::Distance(Point a, Point b):p1(a),p2(b)
{
	int x = a.GetX() - b.GetX();
	int y = a.GetY() - b.GetY();
	dist = sqrt(x * x + y * y);
	cout << "Distance's constructor was called" << endl;
}

double Distance::GetDis()
{
	return dist;
}

Distance::~Distance()
{
	cout << "Distance's destructor was called" << endl;
}

int main()
{
	Point myp1(1, 1), myp2(4, 5);
	Distance myd(myp1, myp2);
	cout << endl;
	cout << "the distance is:" << myd.GetDis() << endl;
	return 0;
}

4.自行编写代码完成自己的String类。注意这里的String字符S大写,主要目的是与C++自带的string类相互区分。

#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;

class String //请勿修改本类的声明,请实现具体的成员函数。

{

public:

	String(const char* str = NULL) //构造函数
	{
		if (str == NULL) {
			mydata = NULL;
			cout << "gouzao" << endl;
		}
		else
		{
			int a = strlen(str);
			mydata = new char(a);
			strcpy(mydata, str);
			cout << "gouzao " << mydata << endl;
		}
	}
	String(const String& r) //拷贝构造函数
	{
		int b = strlen(r.mydata);
		mydata = new char[b];
		strcpy(mydata, r.mydata);
		cout << "kaobei gouzao " << mydata << endl;
	}
	~String()  //析构函数
	{
		if (mydata != NULL) {
			cout << "xigou " << mydata << endl;
			delete(mydata);
		}
		else
		{
			cout << "xigou" << endl;
		}
	}
private:

	char* mydata;  //请勿修改数据成员的类型

};


int main() //请勿修改主函数

{

	String s1, s2("hello");

	String s3(s2);

	return 0;

}

5.根据以下主函数的功能来设计日期类CDateInfo,使其能正确运行。类CDateInfo中应该具有描述年、月、日的三个数据成员和相应的成员函数。

#include<iostream>
using namespace std;
class CDateInfo
{
	int _year;
	int _month;
	int _day;
public:
    CDateInfo(int year, int month, int day)
    {
        _year = year;
        _month = month;
        _day = day;
    }
    CDateInfo()
    {
        _year = 2000;
        _month = 1;
        _day = 1;
    }
	void SetDate(int year, int month, int day)
    {
        _year = year;
        _month = month;
        _day = day;
    }
    void GetDate()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
};

int main()
{
    CDateInfo data1, data2(2011,10,10);  //定义对象data1和data2
    //分别调用类的不带参数的构造函数和带3个参数的构造函数对其数据成员进行初始化
    //date1的数据成员未初始化时,其年月日用默认值2000,1,1来继续初始化
    int y, m, d;
    cin >> y >> m >> d;   //输入年月日值
    data1.SetDate(y, m, d);  //设置data1的年月日为y,m,d
    data1.GetDate();   //按year-month-day的格式显示data1的年月日
    data2.GetDate();    //按year-month-day的格式显示data2的年月日为“2011-10-10”
    return 0;
	return 0;
}

6.根据以下主函数的功能来设计日期类Student,使其能正确运行。类Student中应该具有描述学生姓名、性别、年龄的三个数据成员和相应的成员函数。

#include<iostream>
#include<string.h>
using namespace std;
class Student
{
private:
	char* name;
	int sex;
	unsigned age;
public:
	Student(char* i=NULL,int j=0,unsigned k=0)
	{
		if(i==NULL)
		{
			name=NULL;
			sex=j;
			age=k;
		}
		else
		{
			int a=strlen(i);
			name=new char(a);
			strcpy(name,i);
		    sex=j;
			age=k;
		}
	}
	~Student()
	{
		delete(name);
	}
	void SetName(char* chOne)
	{
		int a=strlen(chOne);
		name=new char(a);
		strcpy(name,chOne);
	}
	void SetGender(int iSex)
	{
		sex=iSex;
	}
	void SetAge(unsigned iOld)
	{
		age=iOld;
	}
	void GetName(char* chOne)
	{
		
	}
	int GetGender()
	{
		return sex;
	}
	unsigned GetAge()
	{
		return age;
	}
};
int main()
{
    Student Zhang_San;  //创建Student类对象Zhang_San
    char* chOne;
    int iSex;
    unsigned iOld;
    chOne = new char[11]; //创建动态字符数组用来存放姓名,指针chOne指向该数组
    cin >> chOne; //输入姓名,存放在chOne所指向的动态字符数组中
    cin >> iSex;  //输入性别,输入1表示性别为“男”,输入0表示性别为“女”
    cin >> iOld;  //输入年龄
    Zhang_San.SetName(chOne);   //用输入的姓名设置对象Zhang_San用来表示姓名的数据成员
    Zhang_San.SetGender(iSex);  //设置对象Zhang_San用来表示性别的数据成员
    Zhang_San.SetAge(iOld); //设置对象Zhang_San用来表示年龄的数据成员
    cout << endl;
    Zhang_San.GetName(chOne);   //调用GetName(char *)成员函数将对象Zhang_San表示姓名的数据成员值
                                //存放到chOne所指向的动态字符数组中
    cout << "Zhang_San's name is " << chOne << endl;  //显示姓名
    cout << "Zhang_San's gender is " << Zhang_San.GetGender() << endl;   //显示性别:1(男),0(女)
    cout << "Zhang_San's age is " << Zhang_San.GetAge() << endl;  //显示年龄
    delete[]chOne;
    return 0;
}

7.根据以下主函数的功能来设计计算器类Calculator,使其能正确运行。类Calculator中应该具有描述运算数的a和b及其表示a和b运算结果的三个数据成员和相应计算并显示结果的成员函数。

#include<iostream>
#include<string.h>
using namespace std;
class Student
{
private:
	char* name;
	int sex;
	unsigned age;
public:
	Student(char* i=NULL,int j=0,unsigned k=0)
	{
		if(i==NULL)
		{
			name=NULL;
			sex=j;
			age=k;
		}
		else
		{
			int a=strlen(i);
			name=new char(a);
			strcpy(name,i);
		    sex=j;
			age=k;
		}
	}
	~Student()
	{
		delete(name);
	}
	void SetName(char* chOne)
	{
		int a=strlen(chOne);
		name=new char(a);
		strcpy(name,chOne);
	}
	void SetGender(int iSex)
	{
		sex=iSex;
	}
	void SetAge(unsigned iOld)
	{
		age=iOld;
	}
	void GetName(char* chOne)
	{
		
	}
	int GetGender()
	{
		return sex;
	}
	unsigned GetAge()
	{
		return age;
	}
};
int main()
{
    Student Zhang_San;  //创建Student类对象Zhang_San
    char* chOne;
    int iSex;
    unsigned iOld;
    chOne = new char[11]; //创建动态字符数组用来存放姓名,指针chOne指向该数组
    cin >> chOne; //输入姓名,存放在chOne所指向的动态字符数组中
    cin >> iSex;  //输入性别,输入1表示性别为“男”,输入0表示性别为“女”
    cin >> iOld;  //输入年龄
    Zhang_San.SetName(chOne);   //用输入的姓名设置对象Zhang_San用来表示姓名的数据成员
    Zhang_San.SetGender(iSex);  //设置对象Zhang_San用来表示性别的数据成员
    Zhang_San.SetAge(iOld); //设置对象Zhang_San用来表示年龄的数据成员
    cout << endl;
    Zhang_San.GetName(chOne);   //调用GetName(char *)成员函数将对象Zhang_San表示姓名的数据成员值
                                //存放到chOne所指向的动态字符数组中
    cout << "Zhang_San's name is " << chOne << endl;  //显示姓名
    cout << "Zhang_San's gender is " << Zhang_San.GetGender() << endl;   //显示性别:1(男),0(女)
    cout << "Zhang_San's age is " << Zhang_San.GetAge() << endl;  //显示年龄
    delete[]chOne;
    return 0;
}

8.设计并测试一个名为Ellipse的椭圆类:

(1)其私有数据成员为外切矩形的左上角与右下角两个点的坐标(4个int型数据成员x1,y1,x2,y2)

(2)声明4个公有的成员函数,分别访问椭圆的外切矩形的顶点坐标,例如int Getx1();

(3)设计一个构造函数Ellipse(int,int,int,int),对椭圆的外切矩形的顶点坐标赋值

(4)设计一个拷贝构造函数。函数原型格式:Ellipse(const Ellipse &e);

(5)设计一个公有成员函数Show(),显示椭圆的外切矩形的顶点坐标。

(6)设计一个公有成员函数Fun(int y),将椭圆外切矩形的左上角和右下角的纵坐标分别加y和减y。

(7)设计一个析构函数,在析构函数中输出“xigou”以及待析构对象的外切矩形左上角的横纵坐标。

8)设计1个公有成员函数Area(),计算椭圆的面积。

#include<iostream>
#include<string.h>
#include<stdio.h>
#define PI 3.1415;
using namespace std;
class Ellipse
{
private:
	int x1, x2, y1, y2;
public:
	int Getx1()
	{
		return x1;
	}
	int Getx2()
	{
		return x2;
	}
	int Gety1()
	{
		return y1;
	}
	int Gety2()
	{
		return x2;
	}
	Ellipse(int xx1, int yy1, int xx2, int yy2)
	{
		x1 = xx1;
		x2 = xx2;
		y1 = yy1;
		y2 = yy2;
	}
	Ellipse(const Ellipse& e)
	{
		x1 = e.x1;
		y1 = e.y1;
		x2 = e.x2;
		y2 = e.y2;
	}
	void Show()
	{
		printf("%d %d %d %d\n", x1, y1, x2, y2);
	}
	void Fun(int y)
	{
		y1 = y1 + y;
		y2 = y2 - y;
	}
	~Ellipse()
	{
		printf("xigou %d %d\n", x1, y1);
	}
	void Area()
	{
		double area;
		area = (double)x2 * y1 * PI;
		if (y1 == 1) 
			printf("%.4lf\n", area);
		else
			printf("%.3lf\n", area);
	}
};

int main()
{
	int a, b, c, d;
	scanf("%d %d %d %d", &a, &b, &c, &d);
	Ellipse i(a, b, c, d);
	Ellipse j(i);
	int num;
	scanf("%d", &num);
	i.Show();
	j.Show();
	i.Area();
	j.Area();
	j.Fun(num);
	j.Area();
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值