类与对象
来源:互联网 发布:易观智库数据准确么 编辑:程序博客网 时间:2024/06/05 10:26
c++中使用struct定义类与class的区别?
类中成员默认的访问权限不同,使用struct定义类时,类中成员的默认属性为public,class定义类时,成员的默认属性为private。
//定义类,创建对象,访问成员
using namespace std;
class Car
{
//成员函数
public:
void disp_welcomemsg();
int get_wheels();
void set_wheels(int);
//数据成员
private:
int m_nwheels;
};
//类成员函数disp_welcomemsg()的实现
void Car::disp_welcomemsg()
{
cout<<"Welcome to the car world!"<<endl;
}
//类成员get_wheels的实现,获取车轮数
void Car::get_wheels()
{
return m_nwheels;
}
//类成员set_wheels()的实现,设置车论数
void Car::set_wheels(int n)
{
m_nwheels=n;
}
int main()
{
Car mycar; //定义类对象
mycar.disp_welcomemsg();//访问成员函数,显示欢迎信息
mycar.set_wheels(4);//访问成员函数,显示车轮信息
cout<<"wheels="<<mycar.get_wheels()<<endl;
system("pause");
return 0;
}
阅读全文
0 0
- OOP-----类与对象对象
- OOP-----类与对象对象
- 对象与元类对象
- 对象与类类型
- C02(类与对象)
- 类与对象(PHP5)
- 类与对象
- 区分类与对象
- 类与对象
- 类与对象
- 类与对象
- C#类与对象
- 对象与类
- 类与对象总结
- js类与对象
- C++ 类与对象
- 类与对象
- 类与对象概述
- 冒泡排序
- 01-C#HelloWorld程序
- QTP VBScript脚本学习
- wget命令
- FreeRTOS 信号量
- 类与对象
- struct和typedef struct彻底明白了 && C语言结构体
- SSIS 获取当天日期
- 13.window滑动窗口以及热点搜索词滑动统计案例实战
- deepmind_lab Getting started on Linux的官方说明
- 虚拟世界和现实生活
- strfttime和sprintf 的区别和用法
- SFTP 使用简写
- html 标签