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

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

时间:2023-11-05 20:59:57浏览次数:29  
标签:const cout 对象 double void 编程 int 实验 include

任务一

task1.cpp

#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===========测试1: string类基础用法===========" << endl;
    test_string();
}
View Code

运行结果

 

任务二

task2.cpp

#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();
}
View Code

运行结果

 

任务三

task3.cpp

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

class T {
    public:
        T(int x = 0, int y = 0);
        T(const T &t); 
        T(T &&t); 
        ~T();
        void set_m1(int x); 
        int get_m1() const;
        int get_m2() const; 
        void display() const; 
        friend void func();
    private:
        int m1, m2;
    public:
        static void disply_count(); 
    public:
        static const string doc; 
        static const int max_count; 
    private:
        static int count; 
};

const string T::doc{"a simple class"};
const int T::max_count = 99;
int T::count = 0;

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;
}

void func() {
    T t1;
    t1.set_m1(55);
    t1.m2 = 77; 
    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

运行结果

 

任务四

task4.cpp

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

class Rect {
    private:
        static int size;
    public:
        static const string doc;
        static int size_info() {
            return size;
        }
        friend void test();
    private:
        double length, width;
    public:
        Rect(double l = 2.0, double w = 1.0) : length{l}, width{w} {
            size++;
        }
        Rect(const Rect &obj) : length{obj.length}, width{obj.width} {
            size++;
        }
        ~Rect() {
            size--;
        }
        double len() const {
            return length;
        }
        double wide() const {
            return width;
        }
        double area() const {
            return length * width;
        }
        double circumference() const {
            return 2 * (length + width);
        }
        void resize(double times) {
            length *= times;
            width *= times;
        }
        void resize(double l_times, double w_times) {
            length *= l_times;
            width *= w_times;
        }
};
int Rect::size = 0;
const string Rect::doc{"a simple Rect class"};

void output(const Rect &r) {
    cout << "矩形信息: " << endl;
    cout << fixed << setprecision(2);
    cout << "长: \t" << r.len() << endl;
    cout << "宽: \t" << r.wide() << endl;
    cout << "面积: \t" << r.area() << endl;
    cout << "周长: \t" << r.circumference() << endl;
}

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

运行结果

 

任务五

task5.cpp

#include <iostream>
#include <cmath>

class Complex {
    private:
        double real, imag;
    public:
        Complex(double r = 0, double i = 0) : real{r}, imag{i} {};
        Complex(const Complex &c) :real{c.real},imag{c.imag} {};
        double get_real() const {
            return real;
        }
        double get_imag() const {
            return imag;
        }
        void add(const Complex &c) {
            real += c.get_real();
            imag += c.get_imag();
        }
        void show() {
            std::cout << real;
            if (imag>0) std::cout << " + " << abs(imag) << 'i';
            if (imag<0) std::cout << " - " << fabs(imag) << 'i';
        }
        void show() const {
            std::cout << real;
            if (imag>0) std::cout << " + " << abs(imag) << 'i';
            if (imag<0) std::cout << " - " << fabs(imag) << 'i';
        }
    public:
        friend Complex add(const Complex&, const Complex&);
        friend bool is_equal(const Complex&, const Complex&);
        friend double abs(const Complex&);
};
Complex add(const Complex &c1, const Complex &c2) {
    double r, i;
    r = c1.get_real() + c2.get_real();
    i = c1.get_imag() + c2.get_imag();
    Complex c(r, i);
    return c;
}
bool is_equal(const Complex &c1, const Complex &c2) {
    bool r, i;
    r = c1.get_real() == c2.get_real();
    i = c1.get_imag() == c2.get_imag();
    return r && i;
}
double abs(const Complex &c) {
    return sqrt(pow(c.get_real(), 2) + pow(c.get_imag(), 2));
}

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) = ";
    cout << 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/NFurioso/p/17811121.html

相关文章

  • 实验三
    task1源代码:#pragmaonce#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);~Point()=default;intget_x()const;intget_y()const;voidshow()const;voidmove(int......
  • js怎么把json字符串转化为一个对象
    在JavaScript中,如果你有一个JSON字符串,你可以使用JSON.parse()方法将其转换成一个JavaScript对象。例如,如果你有以下的JSON字符串:'{"id":1,"name":"Alice"}'你可以使用以下的代码将其转换成一个JavaScript对象://JSON字符串varjsonString='{"id":1,"name"......
  • 实验3 类与数组指针
    task1 point.hpp#pragmaonce#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);~Point()=default;intget_x()const;intget_y()const;void......
  • JUC并发编程学习笔记(十二)Stream流式计算
    Stream流式计算什么是Stream流式计算大数据:存储+计算集合、MySql这些的本质都是存储东西的;计算都应该交给流来操作!一个案例说明:函数式接口、lambda表达式、链式编程、Stream流式计算packageorg.example.stream;importjava.util.Arrays;importjava.util.List;impo......
  • 实验3 C语言函数应用编程
    实验任务1源代码:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);//函数声明8voidprint_spaces(intn);//函数声明9voidprint_blan......
  • 实验三 类与数组、指针
    任务一point.hpp#pragmaonce#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);~Point()=default;intget_x()const;intget_y()const;voidshow()const;voidmove(intnew_x,intnew_y);pri......
  • 实验三
    实验一1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);8voidprint_spaces(intn);9voidprint_blank_lines(intn);1011......
  • Java基础:创建对象有几种方式?
    Java创建对象有几种方式?new关键字  平时使用的最多的创建对象方式Useruser=newUser();反射方式  使用newInstance(),但是得处理两个异常InstantiationException、IllegalAccessException:Useruser=User.class.newInstance();Objectobject=(Object)Class.forName("java.l......
  • 【单片机】初次实验:Keil51的使用
    哔哩哔哩/CSDN/博客园:萌狼蓝天延时器delay(intcount){ inti,j; for(i=0;i<count;i++){ for(j=0;j<1000;j++); }}瞧一瞧题目要求:P0口接八个发光二极管,先让后面四个灯亮,再让前面四个灯亮,循坏#include<REGX51.H>delay(intcount){ inti,j; for(i=0;i<count;i+......
  • 【Go 编程实践】从零到一:创建、测试并发布自己的 Go 库
    为什么需要开发自己的Go库在编程语言中,包(Package)和库(Library)是代码组织和复用的重要工具。在Go中,包是代码的基本组织单位,每个Go程序都由包构成。包的作用是帮助组织代码,提供封装和代码复用的机制。Go包可以包含函数、类型、变量和常量等,这些元素可以被其他包引用和使用。......