首页 > 其他分享 >实验1 类和对象

实验1 类和对象

时间:2023-10-19 23:57:53浏览次数:30  
标签:const cout 对象 double void int 实验 Rect

task 1:
#include <iostream>
#include <string>
#include <vector>
#include <array>
template<typename T>

void output1(const T &obj) {
    for (auto i : obj)
        std::cout << i << ", ";
    std::cout << "\b\b \n";
}
template<typename T>

void output2(const T &obj) {
    for (auto p = obj.begin(); p != obj.end(); ++p)
        std::cout << *p << ", ";
    std::cout << "\b\b \n";
}

void test_array() {
    using namespace std;
    array<int, 5> x1;
    cout << "x1.size() = " << x1.size() << endl;
    x1.fill(42);
    x1.at(0) = 999;
    x1[4] = -999;
    cout << "x1: ";
    output1(x1);
    cout << "x1: ";
    output2(x1);
    array<int, 5> x2{x1};
    cout << boolalpha << (x1 == x2) << endl;
    x2.fill(22);
    cout << "x2: ";
    output1(x2);
    swap(x1, x2);
    cout << "x1: ";
    output1(x1);
    cout << "x2: ";
    output1(x2);
}


void test_vector() {
    using namespace std;
    vector<int> v1;
    cout << v1.size() << endl;
    cout << v1.max_size() << endl;
    v1.push_back(55);
    cout << "v1: ";
    output1(v1);
    vector<int> v2 {1, 0, 5, 2};
    v2.pop_back();
    v2.erase(v2.begin());
    v2.insert(v2.begin(), 999);
    v2.insert(v2.end(), -999);
    cout << v2.size() << endl;
    cout << "v2: ";
    output2(v2);
    vector<int> v3(5, 42);
    cout << "v3: ";
    output1(v3);
    vector<int> v4(v3.begin(), v3.end() - 2);
    cout << "v4: ";
    output1(v4);
}

void test_string() {
    using namespace std;
    string s1{"oop"};
    cout << s1.size() << endl;
    for (auto &i : s1)
        i -= 32;
    s1 += "2023";
    s1.append(", hello");
    cout << s1 << endl;
}

int main() {
    using namespace std;
    cout << "===========测试1: array模板类基础用法===========" << endl;
    test_array();
    cout << "\n===========测试2: vector模板类基础用法===========" << endl;
    test_vector();
    cout << "\n===========测试3: string类基础用法===========" << endl;
    test_string();
}

task 2:
#include <iostream>
#include <complex>

void test_std_complex() {
    using namespace std;
    complex<double> c1{3, 4}, c2{4.5};
    const complex<double> c3{c2};
    cout << "c1 = " << c1 << endl;
    cout << "c2 = " << c2 << endl;
    cout << "c3 = " << c3 << endl;
    cout << "c3.real = " << c3.real() << ", " << "c3.imag = " << c3.imag() << endl;
    cout << "c1 + c2 = " << c1 + c2 << endl;
    cout << "c1 - c2 = " << c1 - c2 << endl;
    cout << "abs(c1) = " << abs(c1) << endl;
    cout << boolalpha;
    cout << "c1 == c2: " << (c1 == c2) << endl;
    cout << "c3 == c2: " << (c3 == c2) << endl;
    complex<double> c4 = 2;
    cout << "c4 = " << c4 << endl;
    c4 += c1;
    cout << "c4 = " << c4 << endl;
}

int main() {
    test_std_complex();
}

task 3:
#include <iostream>
#include <string>
using namespace std;

// 类T的声明
class T {
    public:
        T(int x = 0, int y = 0); // 带有默认形值的构造函数
        T(const T &t); // 复制构造函数
        T(T &&t); // 移动构造函数
        ~T(); // 析构函数
        void set_m1(int x); // 设置T类对象的数据成员m1
        int get_m1() const; // 获取T类对象的数据成员m1
        int get_m2() const; // 获取T类对象的数据成员m2
        void display() const; // 显示T类对象的信息
        friend void func(); // 声明func()为T类友元函数
    private:
        int m1, m2;
    public:
        static void disply_count(); // 类方法,显示当前T类对象数目
    public:
        static const string doc; // 类属性,用于描述T类
        static const int max_count; // 类属性,用于描述T类对象的上限
    private:
        static int count; // 类属性,用于描述当前T类对象数目
};

// 类的static数据成员:类外初始化
const string T::doc{"a simple class"};
const int T::max_count = 99;
int T::count = 0;
// 类T的实现
T::T(int x, int y): m1{x}, m2{y} {
    ++count;
    cout << "constructor called.\n";
}
T::T(const T &t): m1{t.m1}, m2{t.m2} {
    ++count;
    cout << "copy constructor called.\n";
}

T::T(T &&t): m1{t.m1}, m2{t.m2} {
    ++count;
    cout << "move constructor called.\n";
}
T::~T() {
    --count;
    cout << "destructor called.\n";
}
void T::set_m1(int x) {
    m1 = x;
}
int T::get_m1() const {
    return m1;
}
int T::get_m2() const {
    return m2;
}
void T::display() const {
    cout << m1 << ", " << m2 << endl;
}
// 类方法
void T::disply_count() {
    cout << "T objects: " << count << endl;
}
// 友元函数func():实现
void func() {
    T t1;
    t1.set_m1(55);
    t1.m2 = 77; // 虽然m2是私有成员,依然可以直接访问
    t1.display();
}
// 测试
void test() {
    cout << "T class info: " << T::doc << endl;
    cout << "T objects max_count: " << T::max_count << endl;
    T::disply_count();
    T t1;
    t1.display();
    t1.set_m1(42);
    T t2{t1};
    t2.display();
    T t3{std::move(t1)};
    t3.display();
    t1.display();
}

int main() {
    cout << "============测试类T============" << endl;
    test();
    cout << endl;
    cout << "============测试友元函数func()============" << endl;
    func();
}

task 4:
#include <bits/stdc++.h>
using namespace std;
class Rect {
    public:

        Rect(double l = 2, double w = 1);
        Rect(const Rect &r);
        ~Rect();
        double dl() const;
        double dw() const;
        double da() const;
        double dc() const;
        void resize(int flag);
        void resize(int flag0, int flag1);

    private:
        static int size;
        double length, width;

    public:

        static void size_info();
        static const string doc;




};
int Rect::size = 0;
const string Rect::doc = "a simple Rect class";

Rect::Rect(double l, double w) {
    length = l;
    width = w;
    ++size;
}

Rect::Rect(const Rect &r) {
    length = r.length;
    width = r.width;
    ++size;
}

Rect::~Rect() {
    --size;
}

double Rect::dl() const {
    return length;
}

double Rect::dw() const {
    return width;
}

double Rect::da() const {
    return length * width;
}

double Rect::dc() const {
    return 2 * (length + width);
}
void Rect::resize(int flag0, int flag1) {
    length *= flag0;
    width *= flag1;
}
void Rect::resize(int flag) {
    length *= flag;
    width *= flag;
}
void Rect::size_info() {
    cout << "当前矩形对象数目:" << size << endl;
}

void output(const Rect &r) {
    cout << "矩形信息:" << endl;
    cout << "长: " << fixed << setprecision(2) << r.dl() << endl;
    cout << "宽: " << r.dw() << endl;
    cout << "面积: " << r.da() << endl;
    cout << "周长: " << r.dc() << endl;
}

void test() {
    cout << "矩形类信息:" << Rect::doc << endl;
    Rect::size_info();

    Rect r1;
    output(r1);

    Rect r2(4, 3);
    output(r2);

    Rect r3(r2);
    r3.resize(2);
    output(r3);

    r3.resize(5, 2);
    output(r3);

    Rect::size_info();
}

int main() {
    test();
    Rect::size_info();
    return 0;
}

task 5:

 

标签:const,cout,对象,double,void,int,实验,Rect
From: https://www.cnblogs.com/gycgy/p/17776007.html

相关文章

  • 实验2
    task1.c#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;i++){number=rand......
  • 实验二
    实验任务11#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;1314srand(time(0));1516for(i=0;i&......
  • 实验1
    任务1//标准库string,vector,array基础用法#include<iostream>#include<string>#include<vector>#include<array>//函数模板//对满足特定条件的序列类型T对象,使用范围for输出template<typenameT>voidoutput1(constT&obj){for(autoi:obj)st......
  • 实验1 类和对象
    实验任务一:1#include<iostream>2#include<string>3#include<vector>4#include<array>5template<typenameT>6voidoutput1(constT&obj)7{8for(autoi:obj)9std::cout<<i<<",";......
  • 实验一
    任务一点击查看代码#include<iostream>#include<string>#include<vector>#include<array>template<typenameT>voidoutput1(constT&obj){for(autoi:obj)std::cout<<i<<",";std::cout<<&qu......
  • 实验一 类与对象
    Task1:#include<iostream>#include<string>#include<vector>#include<array>template<typenameT>voidoutput1(constT&obj){for(autoi:obj)std::cout<<"i"<<",";std::cout<<......
  • 类和对象
    实验任务1task1.cpp:1//标准库string,vector,array基础用法2#include<iostream>3#include<string>4#include<vector>5#include<array>6//函数模板7//对满足特定条件的序列类型T对象,使用范围for输出8template<typenameT>9voidoutput1(con......
  • 实验1_OOP_22物联网1班_张文瑞
    1.实验任务1:  实验源代码:1//标准库string,vector,array基础用法2#include<iostream>3#include<string>4#include<vector>5#include<array>6//函数模板7//对满足特定条件的序列类型T对象,使用范围for输出8template<typenameT>9voidoutpu......
  • 实验1 类与对象
    实验任务1源代码:1#include<iostream>2#include<string>3#include<vector>4#include<array>567template<typenameT>8voidoutput1(constT&obj){9for(autoi:obj)10std::cout<<......
  • 实验二.
     test1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%(N2-N1......