实验1 类和对象(1)

2022.09.28 OOP实验课

实验1 类和对象(1)

实验任务1

使用C++标准库提供的string类、vector类,观察基于这些类创建对象,通过对象访问接口
的方法。

#include <iostream>
#include <string>
#include <vector>

int main()
{
	using namespace std;
	
	string s1;
	string s2{"c plus plus"};
	string s3{s2};
	string s4 = s2;
	
	s1="oop";
	
	vector<string> v1;
	v1.push_back(s1);
	v1.push_back(s2 + "1");
	v1.push_back(s3 + "2");
	v1.push_back(s4 + "3");
	
	cout << "output1:" << endl;
	for(auto item: v1)
	cout << item << endl;
	
	cout << "output2:" << endl;
	for(auto p = v1.begin(); p != v1.end(); ++p)
	cout << *p << endl;
	
	cout << "output3:" << endl;
	for(auto i=0; i < v1.size(); i++)
	cout << v1[i] << endl;
	
	vector<string> v2{v1.rbegin(),v1.rend()};
	cout << "v2: " << endl;
	for(auto item : v2)
	cout << item << endl;
}

测试结果:

实验任务2

验证性实验。使用C++语言特性中支持面向对象的语法,实现一个Point类来描述点的基础属性和操作。
在C++编码环境中,输入以下代码,结合代码注释和运行结果,熟悉和掌握C++中类的定义、对象的创建
和使用。

#include <iostream>
using std::cout;
using std::endl;

class Point{
	public:
		Point(int x0 = 0, int y0 = 0);
		Point(const Point& p);
		~Point()= default; 
		
		int get_x() const { return x;}
		int get_y() const { return y;}
		void show() const;
	
	private:
		int x, y;
};

void Point::show() const{
cout << "(" << x << "," << y << ")" << endl;
}

Point::Point(int x0, int y0): x{x0}, y{y0}{
cout << "coustructor called."<< endl;
}

Point::Point (const Point& p): x{p.x}, y{p.y}{
cout << "copy constructoe called." << endl;
}

int main()
{
	Point p1(4,5);
	p1.show();
	
	Point p2 = p1;
	p2.show();
	
	Point p3{p2};
	p3.show();
	
	cout << p3.get_x() << endl;
}

测试结果:

实验任务3

验证性实验。使用C++语言特性中支持面向对象的语法,实现一个简单时钟类Clock。通过它实现对时钟
的设置、显示等基础操作。
在C++编码环境中,输入以下代码,结合代码注释和运行结果,熟悉和掌握C++中类的定义、对象的创建
和使用。

#include <iostream>
#include <iomanip>

using std :: cout;
using std :: endl;
//定义时钟类Clock

class Clock{
    public:
    Clock(int h = 0, int m = 0, int s = 0);
    Clock(const Clock& t);
    ~Clock() = default;

    void set_time(int h, int m = 0, int s = 0);
    void show_time() const;

    private:
    int hour, minute, second;
};

//类Clock实现
Clock::Clock(int h, int m, int s):hour{h},minute{m},second{s}{
    cout << "constructor called" << endl;
}

Clock::Clock(const Clock& t):hour{t.hour}, minute{t.minute}, second{t.second}{
    cout << "copy constructor called" << endl;
}

void Clock::set_time(int h, int m, int s){
    hour = h;
    minute = m;
    second = s;
}

void Clock::show_time()const{
    using std::setw;
    using std::setfill;

    cout << setfill('0') << setw(2) << hour << ":"
                         << setw(2) << minute << ":"
                         << setw(2) << second << endl;
}

//普通函数定义
Clock reset(){
    return Clock(0,0,0);//构造函数被调用
}

int main()
{
    Clock c1(12, 0, 5);//构造函数被调用
    c1.show_time();

    c1 = reset();//理论上,复制构造函数被调用
    c1.show_time();
    
    Clock c2(c1);//复制构造函数被调用
    c2.set_time(6);
    c2.show_time();
}

测试结果:

实验任务4

验证性实验。使用C++语言特性中支持面向对象的语法,实现一个抽象类X。它是对类和对象的基础操作的一个抽象概括,覆盖了类的定义、实现、测试,包括各类构造函数、析构函数及其调用时机的观察。

#include <iostream>
class X
{
private:
    int data;
public:
    X();//默认构造函数
    ~X();//析构函数
    X(int m);//构造函数
    X(const X& obj);//复制构造函数
    X(X&& obj)noexcept;//移动构造函数
    void show() const;//显示数据
};

X::X():data{42}
{
    std::cout << "default constructor called.\n";
}

X::~X()
{
    std::cout << "destructor called.\n";
}

X::X(int m): data{m}{
    std::cout << "constructor called.\n";
}

X::X(const X& obj): data{obj.data}{
    std::cout << "copy constructor called.\n";
}

X::X(X&& obj) noexcept: data{obj.data}{
    std::cout << "move constructor called.\n";
}

void X::show() const{
    std::cout << data << std::endl;
}

int main(){
    X x1;//默认构造函数被编译器自动调用
    x1.show();

    X x2{2049};//构造函数被调用
    x2.show();

    X x3{x1};//复制构造函数被调用
    x3.show();

    X x4{ std::move(x2)};//移动构造函数被调用
    x4.show();
}

测试结果:

实验任务5

设计并实现一个矩形类Rectangle,具体要求如下:

  • 数据成员
    length 用于表示矩形长度
    width 用于表示矩形宽度

  • 函数成员
    默认构造函数
    当创建矩形对象时,如果没有指定长和宽,默认矩形长为2.0, 宽为1.0
    比如,主函数中这样定义对象: Rectangle rect1;

    带有两个参数的构造函数
    当创建矩形对象时,如果给定两个参数l和w,则矩形长度length设置为l, 矩形宽度width设置为w
    比如,主函数中这样定义对象: Rectangle rect2(10.0, 5.0);

    复制构造函数
    当创建矩形对象时,用一个已经存在的矩形对象来创建新的
    比如,主函数中这样定义对象: Rectangle rect3(rect1); (其中,rect1是已经定义过的)

    析构函数

    普通函数成员len() 用于返回矩形长度
    普通函数成员wide() 用于返回矩形宽度
    普通函数成员area() 用于计算并返回矩形面积
    普通函数成员circumference() 用于计算并返回矩形周长

    普通重载函数成员resize()
    有一个参数times,用于对矩形长、宽同时缩放times倍

    普通重载函数成员resize()
    有两个参数l_times和w_times, 用于将矩形长度缩放l_times倍,矩形宽度缩放w_times倍

#include <iostream>
#include <iomanip>

class Rectangle
{
private:
    double length,width;
public:
    Rectangle();//默认构造函数
    ~Rectangle();//析构函数
    Rectangle(double l,double w);//构造函数
    Rectangle( const Rectangle& rect);//复制构造函数

    double len() const{ return length; };//普通函数成员len
    double wide() const{ return width; };//普通函数成员wide
    double area() const{ return length*width; };//矩形面积
    double circumference() const{ return 2*(length + width); };//矩形周长

    void resize(double times);
    void resize(double l_times, double w_times);
};

//重载函数定义
void Rectangle::resize(double times){
    length *=times;
    width *= times;
}
void Rectangle::resize(double l_times, double w_times){
    length *= l_times;
    width *= w_times;
}
//类Rectangle的实现(测试)
Rectangle::Rectangle():length{2.0},width{1.0}{
    std::cout << "default constructor called.\n";
}

Rectangle::~Rectangle(){
    std::cout << "destructor called.\n";
}

Rectangle::Rectangle(double l,double w):length{l},width{w}{
    std::cout << "constructor called.\n";
}

Rectangle::Rectangle(const Rectangle& rect):length{rect.length},width{rect.width}{
    std::cout << "copy constructor called.\n";
}
//普通函数,用于输出矩形信息
void output( const Rectangle &rect){
    using namespace std;
    
    cout << "矩阵信息: \n";
    cout <<"长  : "<< fixed << setprecision(2) << setw(4) << rect.len() << endl;//以浮点数形式输出、小数部分保留两位小数
    cout <<"宽  : "<< fixed << setprecision(2) << setw(4) << rect.wide() << endl;
    cout <<"面积: "<< fixed << setprecision(2) << setw(4) << rect.area() << endl;
    cout <<"周长: "<< fixed << setprecision(2) << setw(4) << rect.circumference() << endl;   
    cout << endl;
}

//主函数,测试Rectangle类
int main(){
    Rectangle rect1;//默认构造函数被调用
    output(rect1);

    Rectangle rect2(10, 5);//构造函数被调用
    output(rect2);

    Rectangle rect3(rect1);//复制构造函数被调用
    rect3.resize(2);//重载函数
    output(rect3);

    rect3.resize(5, 2);
    output(rect3);
}

posted @ 2022-10-05 01:46  Aquarius_CHE  阅读(16)  评论(0编辑  收藏  举报