首页 > 编程语言 >实验1 类和对象编程_基础编程1

实验1 类和对象编程_基础编程1

时间:2023-10-16 11:01:15浏览次数:37  
标签:const cout 对象 double void 编程 int 实验 include

实验任务1

task1.cpp

// 标准库string, vector, array基础用法
#include <iostream>
#include <string>
#include <vector>
#include <array>
// 函数模板
// 对满足特定条件的序列类型T对象,使用范围for输出
template<typename T>
void output1(const T &obj) {
    for(auto i: obj)
        std::cout << i << ", ";
    std::cout << "\b\b \n";
}
// 函数模板
// 对满足特定条件的序列类型T对象,使用迭代器输出
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";
}
// array模板类基础用法
void test_array() {
    using namespace std;
    array<int, 5> x1; // 创建一个array对象,包含5个int元素,未初始化
    cout << "x1.size() = " << x1.size() << endl; // 输出元素个数
    x1.fill(42); // 把x1的所有元素都用42填充
    x1.at(0) = 999; // 把下标为0的元素值修改为999
    x1[4] = -999; // 把下表为4的元素值修改为-999
    cout << "x1: ";
    output1(x1); // 调用模板函数output1输出x1
    cout << "x1: ";
    output2(x1); // 调用模板函数output1输出x1
    array<int, 5> x2 {x1};
    cout << boolalpha << (x1 == x2) << endl;
    x2.fill(22);
    cout << "x2: ";
    output1(x2);
    swap(x1, x2); // 交换array对象x1, x2
    cout << "x1: ";
    output1(x1);
    cout << "x2: ";
    output1(x2);
}
// vector模板类基础用法
void test_vector() {
    using namespace std;
    vector<int> v1;
    cout << v1.size() << endl; // 输出目前元素个数
    cout << v1.max_size() << endl; // 输出元素个数之最大可能个数
    v1.push_back(55); // 在v1末尾插入元素
    cout << "v1: ";
    output1(v1);
    vector<int> v2 {1, 0, 5, 2};
    v2.pop_back(); // 从v2末尾弹出一个元素
    v2.erase(v2.begin()); // 删除v2.begin()位置的数据项
    v2.insert(v2.begin(), 999); // 在v1.begin()之前的位置插入
    v2.insert(v2.end(), -999); // 在v1.end()之前的位置插入
    cout << v2.size() << endl;
    cout << "v2: ";
    output2(v2);
    vector<int> v3(5, 42); //创建vector对象,包含5个元素,每个元素值都是42
    cout << "v3: ";
    output1(v3);
    vector<int> v4(v3.begin(), v3.end()-2); // 创建vector对象,以v3对象的[v3.begin(), v3.end()-2)区间作为元素值
    cout << "v4: ";
    output1(v4);
}
// string类基础用法
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();
}
View Code

运行测试结果

实验任务2

task2.cpp

#include <iostream>
#include <complex>
// 测试标准库提供的复数类模板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; // abs()是标准库数学函数,对复数取模
    cout << boolalpha; // 设置bool型值以true/false方式输出
    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();
}
View Code

运行测试结果

实验任务3

task3.cpp

// 一个简单的类T:定义、使用
#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();
T::disply_count();
}
// 主函数
int main() {
cout << "============测试类T============" << endl;
test();
cout << endl;
cout << "============测试友元函数func()============" << endl;
func();
}
View Code

运行测试结果

实验任务4

task4.cpp

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// 矩形类Rect的定义
class Rect{
    public:

    static const std::string doc;

    static int  size_info();

    double length;

    double width;

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

    Rect(const Rect &other) : length(other.length), width(other.width){size++;}

    ~Rect(){
        size--;
    }

    double len(){
        return length;
    }

    double wid(){
        return width;
    }

    double C(){
        return 2 * (length + width);
    }

    double S(){
        return length * width;
    }

    void resize(double times){
        length *= times;
        width *= times;
    }

    void resize(double l_times,double w_times){
        length *= l_times;
        width *= w_times;
    }

// 普通函数:输出矩形信息
void output() {
cout << "矩形信息: " << endl;
cout << fixed << setprecision(2); // 控制输出格式:以浮点数形式输出,小数部分保留两位
// 补足代码:分行输出矩形长、宽、面积、周长
cout << "长:  " << length << endl;
cout << "宽:  " << width << endl;
cout << "面积:" << S() << endl;
cout << "周长:" << C() << endl;
// ×××
}

private:
static int size;
};

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

int Rect::size_info(){
    return size;
}

void output(Rect &r){
    r.output();
}

// 测试代码
void test() {
cout << "矩形类信息: " << Rect::doc << endl;
cout << "当前矩形对象数目: " << Rect::size_info() << endl;
Rect r1;
output(r1);
Rect r2(4, 3);
output(r2);
Rect r3(r2);
r3.resize(2);
output(r3);
r3.resize(5, 2);
output(r3);
cout << "当前矩形对象数目: " << Rect::size_info() << endl;
}
// 主函数
int main() {
test();
cout << "当前矩形对象数目: " << Rect::size_info() << endl;
}
View Code

运行测试结果

实验任务5

task5.cpp

#include <iostream>
#include <cmath>

using namespace std;

class Complex {
private:
    double real;
    double imag;

public:
    Complex(double r = 0, double i = 0) : real(r), imag(i) {}

    Complex(const Complex& other) : real(other.real), imag(other.imag) {}

    double get_real() const {
        return real;
    }

    double get_imag() const {
        return imag;
    }

    void show() const {
        cout << real;
        if(imag != 0){
        if (imag > 0)
            cout << " + " << imag << "i";
        else
            cout << " - " << -imag << "i";
    }
    }

    Complex& add(const Complex& other) {
        real += other.real;
        imag += other.imag;
        return *this;
    }

    friend Complex add(const Complex& c1, const Complex& c2) {
        Complex result(c1);
        return result.add(c2);
    }

    friend bool is_equal(const Complex& c1, const Complex& c2) {
        return (c1.real == c2.real) && (c1.imag == c2.imag);
    }

    friend double abs(const Complex& c1) {
        return sqrt(c1.real * c1.real + c1.imag * c1.imag);
    }
};

void test(){
    using namespace std;

    Complex c1(3, -4);
    const Complex c2(4.5);
    Complex c3(c1);
    cout << "c1 = ";
    c1.show();
    cout << endl;
    cout << "c2 = ";
    c2.show();
    cout << endl;
    cout << "c2.imag = " << c2.get_imag() << endl;
    cout << "c3 = ";
    c3.show();
    cout << endl;
    cout << "abs(c1) = " << abs(c1) << endl;
    cout << boolalpha;
    cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
    cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
    Complex c4;
    c4 = add(c1, c2);
    cout << "c4 = c1 + c2 = ";
    c4.show();
    cout << endl;
    c1.add(c2);
    cout << "c1 += c2, " << "c1 = ";
    c1.show();
    cout << endl;
}

int main() {
    test();
}
View Code

运行测试结果

标签:const,cout,对象,double,void,编程,int,实验,include
From: https://www.cnblogs.com/le-sh/p/17761433.html

相关文章

  • 【Linux 网络编程】为什么 IP 地址通常以192.168开头?——私有 IP 地址段
    首先,192.168并不是设置局域网IP地址的唯一选择。很多企业都选择10.或者172.16开头规划局域网。三个私有IP地址段网络中的主机需要通信,需要使用一个IP地址,目前我们普遍使用的IPv4的地址,分为A、B、C、D、E五类,其中A、B、C类是我们常见的IP地址段。在这三类地址中,大多数为公有地......
  • GO语言中面向接口编程
    接口的基本概念接口是一组行为规范的集合。typeTransporterinterface{//定义接口。通常接口名以er结尾//接口里面只定义方法,不定义变量move(srcstring,deststring)(int,error)//方法名(参数列表)返回值列表whistle(int)int//参数列表和返回值列表......
  • 多线程编程同步:互斥锁和条件变量
    多线程同步怎样同步多个线程或多个进程的活动?为允许在线程或进程间共享数据,同步通常是必需的。而互斥锁和条件变量是同步的基本组成部分。互斥锁用于保护临界区(criticalregion),以保证任何时刻只有一个线程在执行其中的代码,或者任何时刻只有一个进程在执行其中的代码。互斥......
  • 详解.NET依赖注入中对象的创建与“销毁”
    在DI容器中注册类型,DI容器就可以帮我们创建类型的实例;如果注册类型实现了IAsyncDisposable或者IDisposable接口,对象销毁时DI容器还会帮我们调用DisposeAsync或Dispose方法。这是如何实现的呢?一起来看看吧。本文是基于DependencyInjection8.0编写。如果已熟练使用,可以直接从第三......
  • 分享教学项目:开源一个对象映射框架
    Maomi.Mapper项目地址:https://github.com/whuanle/Maomi.Mapper注:本项目用于教学目的,性能较差,请勿用于生产环境。MaomiMapper是一个使用表达式树构造生成对象成员映射的框架,即对象映射框架,用于配合笔者其它系列文章,用于教学目的。笔者此系列教程还没有公开,是讲解如何编写各......
  • Java拾贝第二天——面向对象
    Java拾贝不建议作为0基础学习,都是本人想到什么写什么特性封装性,继承性,多态性。类的组成publicclass类名{//数据类型属性名;intage;//成员变量public返回值类型方法名(传参){ Stringname;//局部变量 //方法体}}其中定义在类中的属性名也就是变......
  • 《Python计算机视觉编程》高清高质量电子书PDF
    下载:https://pan.quark.cn/s/3c386f89afec......
  • 产品代码都给你看了,可别再说不会DDD(七):实体与值对象
    这是一个讲解DDD落地的文章系列,作者是《实现领域驱动设计》的译者滕云。本文章系列以一个真实的并已成功上线的软件项目——码如云(https://www.mryqr.com)为例,系统性地讲解DDD在落地实施过程中的各种典型实践,以及在面临实际业务场景时的诸多取舍。本系列包含以下文章:DDD入门DD......
  • 研发必会-异步编程利器之CompletableFuture(含源码 中)
    微信公众号访问地址:研发必会-异步编程利器之CompletableFuture(含源码中)近期热推文章:    1、springBoot对接kafka,批量、并发、异步获取消息,并动态、批量插入库表;    2、SpringBoot用线程池ThreadPoolTaskExecutor异步处理百万级数据;    3、基于Redis的Geo实现附......
  • 《Unix/linux系统编程》教材第11章学习笔记
    第11章:EXT2文件系统EXT2文件系统Linux一直使用EXT2作为默认文件系统。EXT2文件系统数据结构创建虚拟硬盘mke2fs[-bblksize-Nninodes]devicenblockseg:ddif=/dev/zeroof=vdiskbs=1024count=1440mke2fsvdisk1440在一个名为vdisk的虚拟磁盘文件上创建一个EXT2文......