C++类的应用和构造函数的调用

矩形的周长面积

  1. 定义一个矩形类Rec,包含私有属性length、width,包含公有成员方法:
    1. void set_length(int l); //设置长度
    2. void set_width(int w); //设置宽度
    3. int get_length(); //获取长度,将长度的值返回给调用处
    4. int get_width(); //获取宽度,将宽度的值返回给调用处
    5. void show(); //输出周长和面积

代码

cpp 复制代码
#include <iostream>

using namespace std;

class Rec
{
    const int length;
    int width;
public:
    void set_length(int l);
    void set_width(int w);
    int get_length();
    int get_width();
    void show();
    Rec():length(3),width(4)
    {
        cout<<"无参:"<<endl;
        cout <<"C:"<<2*(length+width)<<endl;
        cout <<"S:"<<length*width<<endl;
    }
    Rec(int width):length(5)
    {
        cout <<"有参"<<endl;
        cout <<"C:"<<2*(length+width)<<endl;
        cout <<"S:"<<length*width<<endl;
    }
};

void Rec::set_width(int width)
{
    this->width=width;
}
int Rec::get_length()
{
    return length;
}

int Rec::get_width()
{

    return width;
}

void Rec::show()
{
    cout <<"C:"<<2*(length+width)<<endl;
    cout <<"S:"<<length*width<<endl;

}
int main()
{
    Rec r1;
    Rec r2(4);
    r2.set_width(6);
    r2.show();
    return 0;
}

实现效果

圆的参数

  1. 定义一个圆类,包含私有属性半径r,公有成员方法:
    1. void set_r(int r); //获取半径
    2. void show //输出周长和面积,show函数中需要一个提供圆周率的参数PI,该参数有默认值3.14

代码

cpp 复制代码
#include <iostream>

using namespace std;

class yuan
{
    int &r;
public:
    void set_r(int r);
    void show();
    yuan(int &r):r(r){
        cout <<"C="<<2*3.14*(float)r<<endl;
        cout <<"S="<<3.14*(float)r*(float)r<<endl;
    }
};

void yuan::set_r(int r)
{
    this->r=r;
}

void yuan::show()
{
    cout <<"C="<<2*3.14*(float)r<<endl;
    cout <<"S="<<3.14*(float)r*(float)r<<endl;
}

int main()
{
    int R=7;
    yuan r1(R);
    return 0;
}

实现效果

汽车的品类

  1. 定义一个Car类,包含私有属性,颜色color,品牌brand,速度speed,包含公有成员方法:
    1. void display(); //显示汽车的品牌,颜色和速度
    2. void acc(int a); //加速汽车
    3. set函数,设置类中的私有属性

代码

cpp 复制代码
#include <iostream>
#include <iomanip>

using namespace std;

class Car
{
    string cloor;
    string ip;
    int speed;
public:
    void display();
    void acc(int a);
    void set_look(string c,string i,int s);
    Car():cloor("black"),ip("别克"),speed(350)
    {
        cout <<"无参的构造函数:"<<endl;
        cout <<"颜色:"<<setw(8)<<left<<cloor<<"品牌:"<<setw(8)<<left<<ip<<"速度:"<<setw(8)<<left<<speed<<endl;
    }
    Car(string cloor,string ip,int speed)
    {
        this->cloor=cloor;
        this->ip=ip;
        this->speed=speed;
        cout <<"有参的构造函数"<<endl;
        cout <<"颜色:"<<setw(8)<<left<<cloor<<"品牌:"<<setw(8)<<left<<ip<<"速度:"<<setw(8)<<left<<speed<<endl;
    }
};

void Car::display()
{
        cout <<"颜色:"<<setw(8)<<left<<cloor<<"品牌:"<<setw(8)<<left<<ip<<"速度:"<<setw(8)<<left<<speed<<endl;
}
void Car::acc(int a)
{
    speed+=a;
}
void Car::set_look(string cloor,string ip,int speed)
{
    this->cloor = cloor;
    this->ip = ip;
    this->speed=speed;
}

int main()
{
    Car bieke;
    Car penz("white","BWM",400);
    penz.set_look("red","benz",360);
    penz.acc(100);
    penz.display();
    return 0;
}

实现效果

知识点思维导图

相关推荐
利刃大大5 分钟前
【在线五子棋对战】二、websocket && 服务器搭建
服务器·c++·websocket·网络协议·项目
喜欢吃燃面13 分钟前
C++刷题:日期模拟(1)
c++·学习·算法
SHERlocked9316 分钟前
CPP 从 0 到 1 完成一个支持 future/promise 的 Windows 异步串口通信库
c++·算法·promise
虚拟之1 小时前
36、stringstream
c++
我很好我还能学1 小时前
【面试篇 9】c++生成可执行文件的四个步骤、悬挂指针、define和const区别、c++定义和声明、将引用作为返回值的好处、类的四个缺省函数
开发语言·c++
蓝婷儿2 小时前
6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础
开发语言·python·学习
渣渣盟2 小时前
基于Scala实现Flink的三种基本时间窗口操作
开发语言·flink·scala
糯米导航2 小时前
Java毕业设计:办公自动化系统的设计与实现
java·开发语言·课程设计
糯米导航2 小时前
Java毕业设计:WML信息查询与后端信息发布系统开发
java·开发语言·课程设计
南岩亦凛汀3 小时前
在Linux下使用wxWidgets进行跨平台GUI开发
c++·跨平台·gui·开源框架·工程实战教程