C++ 第一章 类与对象

类与对象

类型 = 类型数据 + 类型操作

在这里插入图片描述

访问权限

public: 公共访问权限
private:私有访问权限
protected:受保护的访问权限

学习c++的重点:运行时的bug要在编译期找出来

C++语言与C++中的struct

//c语言//
//声明 
struct stu
{
    ...
};
//定义 
struct stu student;
//c++//
//声明
struct stu
{
    ...
};
//定义
1.struct stu student;
2.stu student;

struct 访问权限默认是public
class 访问权限默认是 private

class People{
    public:
    int x, y;
};

struct People2{
    int x, y;
};

如果想在c语言中直接用结构体名定义变量,需要用到 typedef

//typedef的一般用法 typedef type new_type;

特别的当type为用户自定义类型时,type 和 new_type 可以相同。

用于结构体时

typedef struct stu
{
    ...
}Stu;
//定义
1.Stu student;
2.struct stu student;

第一个Class小例子

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
using namespace std;

class People{
    friend int main();//友元#隔壁老宋
    int x, y;
public :
    void set(int x);
    void say();
};

struct People2{
    int x, y;
};

void People::set(int x) {
    cout << "set functiona : " << this << endl;
    this->x = x;
    return;
}

void People::say() {
    cout << x << " " << y << endl;
    return;
}

int main() {
    People a;
    People2 b;
    a.y = 18432;//友元
    cout << "a pbject : " << &a << endl;
    a.set(3);
    b.x = 4;
    a.say();
    cout << b.x << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值