实验1 类和对象(1) 高远

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 using namespace std;
 5 int main() {
 6     string s1;
 7     string s2{ "c ++" };
 8     string s3{ s2 };
 9     string s4 = s2;
10     s1 = "oop";
11     vector<string> v1;
12     v1.push_back(s1);
13     v1.push_back(s2 + "2");
14     v1.push_back(s3 + "3");
15     v1.push_back(s4 + "4");
16 
17     cout << "output" << endl;
18     for (auto item : v1) {
19         cout << item << endl;
20     }
21     cout << "ouput2: " << endl;
22     for (auto p = v1.begin(); p != v1.end(); ++p)
23         cout << *p << endl;
24 
25     cout << "3" << endl;
26     for (auto a = 0; a < v1.size(); ++a)
27         cout << v1[a] << endl;
28     vector<string> v2{ v1.rbegin(), v1.rend() };
29         cout << "v2: " << endl;
30     for (auto item : v2)
31         cout << item << endl;
32     return 0;
33     
34 }

 

        
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <time.h>
using namespace std;
template<typename T>
void output(const T& obj) {
    for (auto item : obj)
        cout << item << ",";
    cout << endl;
    
}
int main() {
    vector<int> v1{ 1, 2, 3, 4 };
    v1.insert(v1.begin(), 2022);
    v1.insert(v1.end(), 2023);
    output(v1);

    v1.pop_back();
    v1.erase(v1.begin());
    output(v1);

    vector<string> v2{ "gaoyuan","nuist" };
    output(v2);
    return 0;
}

#include <iostream>
using namespace std;
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;

};
Point::Point(int x0, int y0) : x(x0), y(y0) {
    cout << "constructor" << endl;
    
}
Point::Point(const Point& p) : x(p.x), y(p.y) {
    cout << "copy" << endl;

    
}
void Point::show() const {
    cout << "x = " << x << " "
        << "y = " << y << endl;
}
int main() {
    Point p1(10, 5);
    p1.show();
    Point p2 = p1;
    p2.show();

    Point p3{ p2 };
    p3.show();
    cout << p3.get_x() << endl;
    return 0;
}

 

 

 

 

 

#include <iostream>
#include <iomanip>
using namespace std;
class Clock {
public:
    Clock(int h = 0, int m = 0, int s = 0);
    Clock(const Clock& c);
    void set_time(int h, int m = 0, int s = 0);
    void show_time() const;
private:
    int hour, min, sec;
};
Clock::Clock(int h , int m , int s ) :hour(h), min(m), sec(s) {
    cout << "constructor" << endl;

};    
Clock::Clock(const Clock& c) : hour(c.hour), min(c.min), sec(c.sec) {

    cout << "copy" << endl;

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

}
Clock reset() {

    return Clock(4, 0, 5);
}
void Clock::show_time() const {
    cout << setfill('0') << setw(2) << hour << " :"
        << setw(2) << min << ":" << setw(2) << sec;
 
}
int main() {
    Clock my_clock(2, 9, 12);
    my_clock.show_time();
    
    Clock new_clock = reset();
    new_clock.show_time();

    Clock c3(my_clock);
    c3.set_time(2);
    c3.show_time();

    return 0;
}

 

#include <iostream>
using namespace std;    
class X {
public:
    X();
    ~X();
    X(int m);
    X(const X& obj);
    X(X&& obj) noexcept;
    void show()const;
private:
    int data;

        
};
X::X() : data{ 42 } {
    cout << "construct default " << endl;

}
X::~X() {
    cout << "destroy" << endl;
}
X::X(int m) :data(m) {

    cout << "constructor" << endl;
}
X::X(const X& obj) : data(obj.data) {
    cout << "copy" << endl;
}
X::X(X&& obj) noexcept : data(obj.data) {
    cout << "move" << endl;
}
void X::show() const {
    cout << data << endl;
}
int main() {
    
        X x1;
        x1.show();
        X x2{ 2049 };
        x2.show(); 
        X x3{ x1 }; 
        x3.show();
        X x4{ std::move(x2) }; 
        x4.show();

}

主函数第一步类对象x1调用默认函数,data = 42,然后show()函数调用。

初始化类对象x2,调用构造函数,data = 2049,然后show()函数调用。

初始化类对象x3,首先调用复制函数,

初始化类对象x4调用移动函数。

运行完主函数的return 0 之后,调用析构函数4次。

 

#include <iostream>
#include <iomanip>
using namespace std;
// 矩形类Rectangle的定义和实现
// 补足代码
// ×××
class Rectangle {
public:
    Rectangle();
    Rectangle(double l, double w);
    Rectangle(Rectangle& p);
    ~Rectangle();
    double len() const {
        return length;
    }
    double wide() {
        return width;
    }
    double area();
    double circumstance();
    void resize(double times);
    void resize(double ltimes, double wtimes);
    void output();
private:
    double length, width;
};
Rectangle::Rectangle() : length(2.0), width(1.0) {};
Rectangle::Rectangle(double l,double w) : length(l), width(w) {};
Rectangle::Rectangle(Rectangle& p) : length(p.length), width(p.width) {};
Rectangle::~Rectangle() = default;
double Rectangle::area() {
    return width * length;
}
double Rectangle::circumstance() {
    return (length + width) * 2;
}
void Rectangle::resize(double times) {
    length *= times;
    width *= times;
}

void Rectangle::resize(double ltimes, double wtimes) {
    length *= ltimes;
    width *= wtimes;
}
// 普通函数, 用于输出矩形信息
void Rectangle::output() {
    using namespace std;
    cout << "矩形信息: \n";
    cout << fixed << setprecision(2); // 控制输出格式:以浮点数形
    cout << "矩形的长:  " << left << setw(6) << len() << endl;
    cout << "矩形的宽:  " << left << setw(6) << wide() << endl;
    cout << "矩形的面积:" << left << setw(6) << area() << endl;
    cout << "矩形的周长:" << left << setw(6) << circumstance() << endl;
    cout << endl;
        // 补足代码:分行输出矩形长、宽、面积、周长
        // ×××
}
// 主函数,测试Rectangle类


int main() {
    Rectangle rect1;// 默认构造函数被调用
    rect1.output();
    Rectangle rect2(10, 5);// 带有两个参数的构造函数被调用
    rect2.output();
    Rectangle rect3(rect1);// 复制构造函数被调用
    rect3.resize(2); // 矩形rect3的长和宽同时缩放2倍
    rect3.output();
    rect3.resize(5, 2);// 矩形rect3的长缩放5倍, 宽缩放2倍
    rect3.output();
}

 

posted @ 2022-10-12 03:46  ABCDEFGOGOGO  阅读(17)  评论(1编辑  收藏  举报