实验3 类和对象

1、输入程序

#include<iostream>
using namespace std;

class Coordinate
{
	public:
		Coordinate(int x1, int y1)
		{
			x = x1;
			y = y1;
		}
		
		Coordinate(Coordinate &p); 
		
		~Coordinate() 
		{
			cout<<"Destructor is called\n";
    	}
    	
		int getx()
		{
			return x;	
		}
		int gety()
		{
			return y;
		}
		
	private:
		int x,y;
};
Coordinate::Coordinate(Coordinate &p)
{
	x = p.x;
	y = p.y;
	cout<<"Copy-initialization Constructor is called\n";
}

int main()
{
	Coordinate p1(3,4);
	Coordinate p2(p1);
	Coordinate p3=p2;
	cout<<"p3 = ("<<p3.getx()<<","<<p3.gety()<<")\n";
	return 0; 
	 
}

(1)写出程序的运行结果。

结果:
在这里插入图片描述
(2)将 Coordinate类中带有两个参数的构造函数进行修改,在函数体内增添下述语句:

cout<< " Constructor is Called. \n"

写出程序的运行结果,并解释输出结果。

结果:在这里插入图片描述
解释:
①创建对象p1时,调用带有两个参数的构造函数,输出第一行结果;
②创建对象p2、p3时,调用拷贝构造函数,输出第2、3行结果;
③程序运行结束,释放对象p1、p2、p3,分别调用析构函数输出最后3行结果。

(3)按下列要求进行调试:在主函数体内,添加下列语句

Coordinate p4;
Coordinate p5(2);

调试程序时会出现什么错误?为什么?如何对已有的构造函数进行适当修改?

①错误:
[Error] no matching function for call to ‘Coordinate::Coordinate()’
[Error] no matching function for call to ‘Coordinate::Coordinate(int)’
②原因:
类中没有提供适合创建对象p4、p5的构造函数。
③修改:
将构造函数改为:

 Coordinate(int x1=0,int y1=0)

(4)经过以上第(2)步和第(3)步的修改后,结合运行结果分析:创建不同的对象时会调用不同的构造函数。

结果:
在这里插入图片描述
分析:
创建对象p1、p4、p5时,调用3次构造函数;
创建对象p2、p3时,调用2次拷贝构造函数。


2、设计一个4*4魔方程序,让魔方的各行值和等于各列值的和,并且等于两对角线值的和。例如以下魔方:
   31    3    5   25
    9   21   19   15
   17   13   11   23
    7   27   29    1
各行、各列以及两对角线值的和都是64。

#include <iostream>
#include <iomanip>
using namespace std;

class magic
{
public:
    void getdata();       //设置初始成员函数
    void setfirstmagic(); //设置初始魔方成员函数
    void generatemagic(); //生成最终魔方成员函数
    void printmagic();    //显示魔方成员函数
private:
    int m[4][4];
    int step;  //相邻元素之间的差值
    int first; //起始值
    int sum;   //最大元素值和最小值元素的和
};
void magic::getdata() 
{
    cout << "输入4*4魔方起始值:";
    cin >> first;
    cout << "输入相邻元素差值:";
    cin >> step;
}

void magic::setfirstmagic()
{
    int i, j;
    int tmp;
    tmp = first;
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            m[i][j] = tmp;
            tmp += step;
        }
    }
}

void magic::generatemagic()
{
    sum = m[0][0] + m[3][3];
    for (int i = 0, j = 0; i < 4; i++, j++)
        m[i][j] = sum - m[i][j];
    for (int i = 0, j = 3; i < 4; i++, j--)
        m[i][j] = sum - m[i][j];
}

void magic::printmagic()
{
    int i, j;
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            cout << setw(5) << m[i][j];
        }
        cout << endl;
    }
}

int main()
{
    magic A;
    A.getdata();
    A.setfirstmagic();
    cout << "初始魔方如下:" << endl;
    A.printmagic();
    A.generatemagic();
    cout << "最终魔方如下:" << endl;
    A.printmagic();
    return 0;
}

3、设计一个用来表示直角坐标系的Location类,在主程序中创建类 Location的两个对象A和B,要求A的坐标点在第3象限,B的坐标点在第2象限,分别采用成员函数和友元函数计算给定两个坐标点之间的距离,要求按如下格式输出结果:果A(x1,y1),B(x2,y2)
Distance1=d1
Distance2=d2
其中:x1、y1、x2、y2为指定的坐标值,d1和d2为两个坐标点之间的距离。

#include <iostream>
#include <cmath>
using namespace std;

class Location
{
public:
    Location(double, double); //构造函数
    double Getx()             //成员函数,取x坐标的值
    {
        return x;
    }
    double Gety()
    {
        return y;
    }
    double distance(Location &);                    //成员函数,求给定两点之间的距离
    friend double distance(Location &, Location &); //友元函数,求给定两点之间的距离
private:
    double x, y;
};
Location::Location(double a, double b)
{
    x = a;
    y = b;
}
double Location::distance(Location &p)
{
    double dx = x - p.x;
    double dy = y - p.y;
    return (double)sqrt(dx * dx + dy * dy);
}
double distance(Location &p1, Location &p2)
{
    double dx = p1.x - p2.x;
    double dy = p1.y - p2.y;
    return sqrt(dx * dx + dy * dy);
}
int main()
{
    Location A(-1, -2), B(-1, 1);
    cout << "A(" << A.Getx() << "," << A.Gety() << "),B(" << B.Getx() << "," << B.Gety() << ")" << endl;
    cout << "Distance1=" << A.distance(B) << endl;
    cout << "Distance2=" << distance(A, B) << endl;
    return 0;
}

4、声明一个 Student类,在该类中包括一个数据成员 score(分数)、两个静态数据成员total score(总分)和 count(学生人数);还包括一个成员函数 account()用于设置分数、累计学生的成绩之和、累计学生人数,一个静态成员函数sum()用于返回学生的成绩之和,另个静态成员函数 average()用于求全班成绩的平均值。在main函数中,输人某班同学的成绩,并调用上述函数求出全班学生的成绩之和和平均分。

#include <iostream>
using namespace std;

class Student{
    public:
        void account(double s);
        static double sum();
        static double average();
    private:
        double score;
        static double total_score;
        static double count;
};
double Student::total_score=0;
double Student::count=0;
void Student::account(double s)
{
    score=s;
    total_score+=score;
    count++;
}
double Student::sum()
{
    return total_score;
}
double Student::average()
{
    return total_score/count;
}

int main()
{
    Student stu[10];
    int n;
    double s;
    cout<<"请输入学生的人数(1~10):";
    cin>>n;
    for(int i=0; i<n; i++){
        cout<<"请输入第"<<i+1<<"个学生的成绩:";
        cin>>s;
        stu[i].account(s);
    }
    cout<<"所有学生的成绩之和:"<<Student::sum()<<endl;
    cout<<"学生的平均成绩:"<<Student::average()<<endl;
    return 0;
}


5、使用C++的 string类,将5个字符串按逆转后的顺序显示出来。例如,逆转前的5个字符串是:
Germany Japan America Britain France
按逆转后的顺序输出字符串是:
France Britain America Japan Germany

#include<iostream>
#include<string>
using namespace std;
const int len=5;
void Reverse(string s[]);
int main()
{
    string str[len]={"Germany","Japan","American","Britain","France"};
    cout<<"逆转前:"<<endl;
    for(int i=0; i<len; i++)
        cout<<str[i]<<" ";
    cout<<endl;
    cout<<"逆转后:"<<endl;
    Reverse(str);
    for(int i=0; i<len; i++)
        cout<<str[i]<<" ";
    cout<<endl;
    return 0;
}
void Reverse(string s[])
{
    string t;
    for(int i=0; i<len/2; i++)
    {
        t=s[i];
        s[i]=s[len-i-1];
        s[len-i-1]=t;
    }
}
  • 8
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值