C++类-成员,成员函数,构造函数,析构函数

简介: C++类-成员,成员函数,构造函数,析构函数

C++类

成员,成员函数说明:

#include<iostream>
using namespace std;
class box
{
public:
  // 变量 成员
  double length = 2;
  double breadth;
  double height;
  // 方法 成员函数 
  double get();
  void set(double len, double bre, double hei);
};
double box::get()
{
  return length * breadth * height;
}
void box::set(double len, double bre, double hei)
{
  length = len;
  breadth = bre;
  height = hei;
}
int main()
{
  box box1, box2, box3;
  double volume = 0.0;
  // box1的描述
  box1.length = 5.0; box1.breadth = 6.0; box1.height = 7.0;
  // box2的描述
  box2.length = 10; box2.breadth = 12; box2.height = 13;
  volume = box1.length * box1.breadth * box1.height;
  cout << "box1的体积为:" << volume << endl;
  volume = box2.length * box2.breadth * box2.height;
  cout << "box2的体积为:" << volume << endl;
  // box3的相关描述
  box3.length = 1; box3.breadth = 2; box3.height = 3;
  volume = box3.get();
  cout << "box3的体积为:" << volume << endl;
  getchar();
  return 0;
}

修饰符和派生:

#include <iostream>
using namespace std;
class Box
{
private:
  double length; // 默认的是private类型,只能在类内部进行使用
protected:
  double width; // protected成员在类外部不可以被调用,但是在子类/派生类中可以被调用
public:
  double height; // 可以通过 .  直接进行访问
  // 成员函数,定义长度和宽度
  void setwidth(double wid);
  void setlength(double len); // 定义获取长度的成员函数
  // 成员函数  获取长度和宽度
  double getwidth()
  {
    return width;
  }
  double getlenth()
  {
    return length;
  }
};
void Box::setwidth(double wid)
{
  width = wid;
}
void Box::setlength(double len)
{
  length = len;
}
class SmallBox :Box
{
public:
  void setSmallBox(double wid, double hei);
  double SmallBox::getSmallWidth()
  {
    return width;
  }
  double SmallBox::getSmallHeight()
  {
    return height;
  }
};
void SmallBox::setSmallBox(double wid, double hei)
{
  // length是private类型,不能访问
  width = wid; // protected 类型修饰符 可以访问
  height = hei; // 公共  可以访问
}
int main()
{
  Box box1;
  SmallBox sbox1;
  // 获取长度和宽度
  box1.setlength(3); 
  box1.setwidth(2);
  // 获取高度
  box1.height = 1;
  // 访问子类
  // 设置子类的宽度和高度,但是长度是private类型,不能访问
  sbox1.setSmallBox(5,6);
  sbox1.getSmallWidth();
  cout << "box1 的长宽高为:" << box1.getlenth() << " " << box1.getwidth() << " "<< box1.height << endl;
  cout << "sbox的宽和高为:" << sbox1.getSmallWidth() << " " << sbox1.getSmallHeight() << endl;
  getchar();
  return 0;
}

构造函数,析构函数

#include <iostream>
using namespace std;
class Line
{
public:
  void setLineLength(double len);
  double getLineLength()
  {
    return length;
  }
  Line(double len); // 构造函数
  ~Line(); // 析构函数
private:
  double length;
};
void Line::setLineLength(double len)
{
  length = len;
  cout << "设置直线的长度为:" << len << endl;
}
Line::Line(double len) :length(len)
{
  cout << "创建了对象,长度为:" << len << endl;
}
Line::~Line()
{
  // 在程序结束的时候会执行析构函数进行删除对象,即return 0之后,可以在命令行中查看
  cout << "删除了对象" << endl; 
}
int main()
{
  Line l(1); // 构造函数设置线段初始值
  cout << "构造函数设置的线段长度:" << l.getLineLength() << endl;
  l.setLineLength(2); // 成员函数设置初始值
  cout << "成员函数设置的线段长度:" << l.getLineLength() << endl;
  getchar();
  return 0;
}
目录
相关文章
|
3天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
19 0
|
3天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
19 0
|
2天前
|
设计模式 Java C++
【C++高阶(八)】单例模式&特殊类的设计
【C++高阶(八)】单例模式&特殊类的设计
|
2天前
|
编译器 C++
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
|
28天前
|
存储 C++ 容器
C++入门指南:string类文档详细解析(非常经典,建议收藏)
C++入门指南:string类文档详细解析(非常经典,建议收藏)
37 0
|
28天前
|
存储 编译器 C语言
C++入门: 类和对象笔记总结(上)
C++入门: 类和对象笔记总结(上)
33 0
|
6天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
8天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
8天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”

热门文章

最新文章