C++ class类(四)

32 篇文章 0 订阅
  1. 设计一个类
    1. class  + 类名 {  成员变量  成员函数   }
    2. 将属性和行为作为一个整体,来表现生活中的事物
    3. 将属性和行为  加以权限控制
    4. 访问权限
      1. 公共权限 public    类内 类外  都可以访问
      2. 私有权限 private   类内可以访问  类外不可以访问
      3. 保护权限 protected类内可以访问  类外不可以访问
    5. class 默认权限  私有权限  而 struct默认权限是 公共权限
    6. 通过类创建对象过程   称为 实例化对象
  2. 函数的默认参数和占位参数
    1. 默认参数
      1. 可以给函数的形参添加默认值
      2. 语法  形参  类型 变量  = 默认值
      3. int func(int a, int b = 10 , int c = 10)
      4. 注意事项 ,如果有一个位置有了默认参数,那么从这个位置起,从左到右都必须有默认值
      5. 函数的声明和实现 只能有一个 提供默认参数,不可以同时加默认参数
    2. 占位参数
      1. 只写一个类型进行占位,调用时候必须要传入占位值
      2. void func2(int a , int = 1)
      3. 占位参数也可以有默认值
  3. 函数重载
    1. 满足条件
      1. 同一个作用域下
      2. 函数名称相同
      3. 函数参数个数、类型、顺序不同
    2. 函数的返回值  不可以作为重载条件
    3. 注意事项
      1. 加const和不加const的引用可以作为重载条件
      2. 函数重载碰到默认参数  注意避免二义性出现
  4. extern C 
    1. 用途:在C++中调用C语言文件
    2. C++中有函数重载,会对函数名称做修饰,导致调用C语言的函数链接失败
    3. 利用extern C可以解决问题
      1. 方法1:
        1. 在C++代码中加入
        2. 告诉编译器  show函数用C语言方式 做链接
        3. //extern "C" void show();
      2. 方法2:
        1. 在C语言的头文件中加入6行代码
        2. #ifdef __cplusplus  // 两个下划线  __  c plus plus
        3. extern "C" {
        4. #endif
        5. #ifdef __cplusplus  // 两个下划线  __  c plus plus
        6. }
        7. #endif         

 示例

#include <iostream>
#include<string>
#include"test.h"

using namespace std;

const double PI = 3.1415926;


// 内联函数,在函数前加inline,以空间换时间,可以避免预处理中宏的一些问题
//inline void func
//class类中函数默认为内联函数

//以下情况会将内联函数当层普通函数处理
//存在循环
//过多判断
//函数过于庞大
//不能对函数取地址

class circle {
//私有属性,类内使用
private:
    int r;
//公有属性,随便用
public:
    int getR() {
        return this->r;
    }
    void setR(int data) {
        this->r = data;
    }
    double calc() {
        return this->r*PI;
    }
//受保护属性,类内和子类
protected:
};

//引用c语言中方法
//extern "C" void show();

//函数带默认参数,不写函数名为站位符
//a后面要全带默认值
//void _test_int_int_int_int_int(){}编译时会对函数名称从新修饰

void test(int a1,int,int a=3,int b=4,int=6) {
    show();
}


class Person {
private:
    string name;
    int age;
    string car;
    int deposit;
public:
    //构造函数,类创建时执行
    Person(string name, int age, string car, int deosit);
    //析构函数,类释放前执行
    ~Person();
    void showPersonInfo() {
        cout << "name:" << this->name << endl;
        cout << "age:" << this->age << endl;
        cout << "car:" << this->car << endl;
        cout << "deposit:" << this->deposit << endl;
    }
protected:
};

Person::Person(string name,int age,string car,int deposit) {
    this->name = name;
    this->age = age;
    this->car = car;
    this->deposit = deposit;
}

Person::~Person() {
    cout<<"reuslt"<<endl;
}

//函数重载,根据函数参数重载,不可以根据返回值重载(返回值可以不要)
void test() {
    Person* p = new Person("Andy", 21, "保时捷", 9999);
    p->showPersonInfo();
    delete p;
}


int main()
{
    circle* c = new circle();
    c->setR(4);
    cout <<"r:" << c->getR() << endl;
    cout << c->calc()<< endl;
    test(3,4,5);
    test();
    system("pause");
    return EXIT_SUCCESS;
}

test.h 

#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include<stdio.h>

	void show();

	void hide();

#ifdef __cplusplus
}
#endif // __cplusplus

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值