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

实验1 类和对象

时间:2023-10-18 23:44:09浏览次数:24  
标签:const 对象 double void int Complex 实验 Rect

 


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

 

#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 = " << abs(c1) << endl;
    
    cout << boolalpha;
    cout << "c1 == c2:" << (c1 == c2) << endl;
    cout << "c3 == c2:" << (c3 == c2) << endl;
    
    complex<double> c4 = 2;
    cout << "c4 = " << c2 << endl;
    c4 += c1;
    cout << "c4 = " << c4 << endl;
}

int main() {
    test_std_complex();
}

 

#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 << "==========测试类==========" << endl;
    test();
    cout << endl;
    
    cout << "==========测试友元函数func()=========="  << endl;
    func();
}

 

#include <iostream> 
#include <string>
#include <iomanip>

using namespace std;

class Rect
{
public:
    Rect();
    Rect(int l, int w);
    Rect(const Rect &r);
    
    double len()const{return lenth;}
    double wide()const{return width;}
    double area()const{return lenth*width;}
    double circumference()const{return (lenth+width)*2;}
    void resize(double times);
    void resize(double l_times, double w_times);
    
    static string doc;
    static int size_info(){return count;}
    ~Rect() {count--;}
    
private:
    double lenth, width;
    static int count;
};

Rect::Rect(){
    lenth = 2.0;
    width = 1.0;
    count++;
}

Rect::Rect(int l, int w){
    lenth = l;
    width = w;
    count++;
}

Rect::Rect(const Rect &r){
    lenth = r.lenth;
    width = r.width;
    count++;
}


void Rect::resize(double times){
    lenth *= times;
    width *= times;
}

void Rect::resize(double l_times, double w_times){
    lenth *=l_times;
    width *=w_times;
}

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

void output (const Rect &r) {
    cout << "矩形信息:" << endl;
    cout << fixed << setprecision(2);
    cout << "长:   " << left << r.len() << endl;
    cout << "宽:   " << left << r.wide() << endl;
    cout << "面积: " << left << r.area() << endl;
    cout << "周长: " << left << 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;
}

 

#include <iostream> 
#include <cmath>


using namespace std;


class Complex{
public:
    Complex();
    Complex(double real0);
    Complex(double real0,double imag0);
    Complex(const Complex &x);
    
    double get_real()const {return real;}
    double get_imag()const {return imag;}
    void show()const;
    void add(const Complex &x);
    
    friend Complex add(Complex x1, Complex x2);
    friend bool is_equal(const Complex &x1,const Complex &x2);
    friend double abs(const Complex &x);
    
private:
    double real,imag;
};

Complex::Complex(){
    real = 0;
    imag = 0;
}

Complex::Complex(double real0){
    real = real0;
    imag = 0;
}

Complex::Complex(double real0,double imag0) {
    real = real0;
    imag = imag0;
}

Complex::Complex(const Complex &x){
    real = x.real;
    imag = x.imag;
}


void Complex::show() const {
    
    using namespace std;
    if(real == 0 && imag == 0) {cout << 0;} 
    else if(real == 0){cout << imag << "i"; } 
    else if(imag == 0){cout << real;}
    else {
        if (imag > 0){
            cout << real << " + " << imag << "i" ;
    }
        else if(imag < 0) {
            cout << real << " - " << -imag << "i" ;
}
}
}

void Complex::add(const Complex &x){
    real = real+x.real;
    imag = imag+x.imag;
} 

Complex add(Complex x1, Complex x2) {
    double real,imag;
    x1.real = x1.real + x2.real;
    x2.imag = x1.imag + x2.imag;
    return x1;
}

bool is_equal(const Complex &x1, const Complex &x2) {
    bool real,imag;
    real = x1.real == x2.real;
    imag = x1.imag == x2.imag;
    return real && imag;
}
double abs(const Complex &x) {
    return sqrt(pow(x.real, 2) + pow(x.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();
}

 

标签:const,对象,double,void,int,Complex,实验,Rect
From: https://www.cnblogs.com/yzsya/p/17766956.html

相关文章

  • 实验二
    一、实验二作业①:要求:在中国气象网(http://www.weather.com.cn)给定城市集的7日天气预报,并保存在数据库。输出信息序号地区日期天气信息温度1北京7日(今天)多云转晴31℃/17℃2北京8日(明天)晴34℃/20℃3北京9日(后天)多云转晴32℃/19℃4北京10日(......
  • 实验2 C语言分支与循环基础应用编程
    实验任务1 #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-N......
  • GC Roots的对象有哪些?
    简单来说,作为GCRoots的主要有四种对象:虚拟机栈(栈帧中的本地变量表)中引用的对象方法区中类静态属性引用的对象方法区中常量引用的对象本地方法栈中JNI引用的对象 高级回答:1.虚拟机栈(栈帧中的本地变量表)中引用的对象;各个线程调用方法堆栈中使用到的参数、局部变量、临......
  • 204 K8S API资源对象介绍03 (Job CronJob Endpoint ConfigMap Secret) 2.12-2.16
    一、API资源对象Job一次性运行后就退出的Pod1.1使用kubect生成YAML文件#kubectlcreatejobjob01--image=busybox--dry-run=client-oyaml>job01.yaml#vimjob01.yaml#catjob01.yamlapiVersion:batch/v1kind:Jobmetadata:creationTimestamp:nullnam......
  • 钉钉圈子群用于实验室辅助管理
    其实前面一直用企业微信管理实验室的,疫情期间企业微信未认证的人数上限是500,也差不多够用了。上半年开始企业微信开始弹认证提醒了,提示人数不能超100,然后就发现很多新加的学生无法发送信息了,审批功能倒还是能用,就是不能聊天。微信群、freeflarum免费论坛、兔小巢、QQ频道,学生都没......
  • WPF性能优化:Freezable 对象
    Freezable是WPF中一个特殊的基类,用于创建可以冻结(Freeze)的可变对象。冻结一个对象意味着将其状态设置为只读,从而提高性能并允许在多线程环境中共享对象。Freezable的应用我们定义画刷资源的时候常常会这样写:<SolidColorBrushx:Key="RedBrush"Color="Red"o:Freeze="True"/>......
  • 实验1 类和对象_基础编程1
    实验任务1task1.cpp1//标准库string,vector,array基础用法23#include<iostream>4#include<string>5#include<vector>6#include<array>78//函数模板9//对满足特定条件的序列类型T对象,使用范围for输出10template<typenameT>11v......
  • JS数组对象合并,a,b 合并为c
    vara=[{id:2,nickname:"韩信",checked:false},{id:7,nickname:"刘邦",checked:true},];varb=[{id:2,nickname:"韩信",checked:false},{id:7,nickname:"刘邦",checked:false},{id:8,nickname:&......
  • 如何限制类对象只能建立在堆上或者栈上?
    整理至:链接在C++中,类的对象建立分为两种,一种是静态建立,如Aa;另一种是动态建立,如Aptr=newA;这两种方式是有区别的。栈上:编译器在栈上分配内存,然后调用构造函数初始化内存空间堆上:调用new分配合适的堆内存,然后调用构造函数初始化内存空间1、只能建立在堆上方法一:将构造函数......
  • 实验二 Linux命令使用(二)
    实验内容及步骤:(1)进入家目录,创建自己的子目录,进入该子目录,运行date>file1,然后运行catfile1,看到什么信息?“>”是什么符号?答:输出重定向操作符解释“date>file1”的含义:答:将当前日期和时间写入名为file1的文件中。(2)运行mandate>>file1,再运行catfile1,看到什么?mandat......