【C++】类与对象

类的声明

class 类名称
{
public:
   公有成员(外部接口)
private:
   私有成员
protected:
   保护成员
};

/*Clock.h
*实现简单的时钟类,变量包括时分秒
*/
#ifndef _CLOCK_H
#define _CLOCK_H
class Clock
{
public:
    void Display();  //显示时分秒
    void Init(int hour,int minute,int second);//输入时间

    void Update(); //更新时间

    int getHour_(); //得到时
    int getMinute_(); //分
    int getSecond_(); //秒

    void setHour_(int hour);  //输入 时
    void setMinute_(int minute); //分
    void setSecond_(int second); //秒

private:
    int     hour_;
    int     minute_;
    int     second_;

};
#endif
/*Clock.cpp
*各函数的具体实现
*/
#include "Clock.h"
using namespace std;

void Clock::Display()
{
    cout << hour_ << ":" << minute_ << ":" << second_  << endl;
}
void Clock::Init(int hour,int minute,int second)
{
    hour_ = hour;
    minute_ = minute;
    second_  = second;
}
void Clock::Update()
{
    second_ ++;
    if(second_ == 60)
    {
        minute_ ++;
        second_ = 0;
    }
    if(minute_ == 60)
    {
        hour_ ++;
        minute_ = 0;
    }
    if(hour_ == 24)
    {
        hour_ = 0;
    }
}
int Clock::getHour_()
{
    return hour_;
}

int Clock::getMinute_()
{
    return minute_;
}
int Clock::getSecond_()
{
    return second_;
}

void Clock::setHour_(int hour)
{
    hour_ = hour;
}
void Clock::setMinute_(int minute)
{
    minute_ = minute;
}
void Clock::setSecond_(int second)
{
    second_ = second;   
}
#include "Clock.h"
//using namespace std;

int main()
{
    Clock c;
    c.Init(10,10,10);
    c.Display();

    /*当把变量设为私有后,外部函数不能访问该变量*/
    //c.second_ += 1;  
    /*定义接口函数*/
    c.Update();

    /*访问变量*/
    c.Display();

    //c.getMinute_(); 
    c.setHour_(12);
    c.Display();
    return 0;
}

公有、私有、保护成员

public:它们是类与外部的接口,任何外部函数都可以访问公有类型数据和函数

private:只允许本类中的函数访问,而类外部的函数不能访问

protected:与private类似,其差别表现在继承与派生时对派生类的影响不同

数据抽象和封装

  类设计者必须关心类是如何实现的;使用者只要抽象地考虑该类型做什么,而不必具体地考虑该类内部是怎么实现的,只要类所提供的接口不变,那么对类使用者所写的代码就是稳定的。
  封装是一项将低层次的元素组合起来形成更高层次的实体的技术。函数是封装的一种形式:函数所执行的细节行为被封装在函数这个更大的实体中。被封装的元素隐藏了它们的实现细节--可以调用函数,但是不能直接访问函数所执行的语句。类也是一个封装的实体:它代表若干成员的聚集,设计良好的类隐藏了类实现的细节。

内联成员函数

内联函数的两种实现方式:
1.在成员函数具体实现时,在返回类型前加  **inline** 关键字
/*Test.cpp*/
#include "Test.h"
using namespace std;
inline int Test::Add(int a,int b)
{
    return a + b;
}
2.直接在类体中给出实现
class Test{
public:   //内联函数第二种定义方式
    int Add(int a,int b)
    {
        return a + b;
    }
};
成员函数的重载
/*类的声明*/
#ifndef _TEST_H
#define _TEST_H

class Test{
public:
    /*构成重载*/
    void Init();
    void Init(int x);
    void Init(int x,int y);
    void Init(int x,int y,int z);

    void Display();

private:
    int x_;
    int y_;
    int z_;
};
#endif
/*成员函数的具体实现*/
void Test::Init()
{
    x_ = 0;
    y_ = 0;
    z_ = 0;
}
void Test::Init(int x)
{
    x_ = x;
    y_ = 0;
    z_ = 0;
}
void Test::Init(int x,int y)
{
    x_ = x;
    y_ = y;
    z_ = 0;
}
void Test::Init(int x,int y,int z)
{
    x_ = x;
    y_ = y;
    z_ = z;
}
void Test::Display()
{
    cout << "x=" << x_ << endl;
    cout << "y=" << y_ << endl;
    cout << "z=" << z_ << endl;
}
/*主函数*/
#include "Test.h"

using namespace std;
int main()
{
    Test t;
    t.Init();
    t.Display();
    t.Init(10);
    t.Display();
    return 0;
}
/*输出:*/
x=0
y=0
z=0
x=10
y=0
z=0
附:成员函数可以有默认缺省值

class 与 struct 的区别

在未指定访问权限时,class默认的是私有的,struct 默认是公有的
struct Test1
{
    int x_;
    int y_;
    void Init(int x,int y)
    {
        x_ = x;
        y_ = y;
    }
    void Display()
    {
        cout << x_ << "," << y_ <<endl;
    }
};
class Test2
{
    int x_;
    int y_;
    void Init(int x,int y)
    {
        x_ = x;
        y_ = y;
    }
    void Display()
    {
        cout << x_ << "," << y_ <<endl;
    }
};
int main()
{
    Test1 test1;
    Test2 test2;
    /*结构体*/
    test1.Init(10,10);
    test1.Display();

    /*类--默认是私有 所以error*/
    //test2.Init(20,20);
    //test2.Display();

    return 0;
}

前向声明

C++ 中类必须先定义,才能够实例化。
两个类需要相互引用形成一个"环形"引用时,无法先定义使用。这时候需要用到前向声明。
#ifndef _前向声明A_H
#define _前向声明A_H

#include "前向声明B.h"  //用到B类对象所以包含头文件
class A
{
public:
    A(void);
    ^A(void);

    B b_;
};
#endif
/*A.h用到B.h,B.h也用到A.h,这是错误的,这时候就要用到前向声明*/
#ifndef _前向声明B_H
#define _前向声明B_H
//#include "前向声明A.h"     前向声明后,头文件不需要包含头文件
class A;  
class B
{
public:
    B(void);
    ^B(void);

/*类B中也用到A类对象,这就是环形引用*/
    A a_;//error :不能实例化对象,也不能用作参数,能定义指针或引用
};

#endif

局部类(一般很少使用)

1.类也可以定义在函数体内,这样的类称为局部类。
2.局部类只在定义它的局部域内可见
3.局部类只能在定义它的函数内部实现和使用,即在函数内有效
4.局部类内部不能定义静态成员。
void Fun()
{
    class LocalClass
    {
    public:
        int num_;
        void Init(int num)
        {
            num_ = num;
        }
        void Display()
        {
            cout << num_ << endl;
        }   
        static int num2_; //error 局部类内不能定义静态成员
    };
    /*函数体内部使用*/    
    LocalClass lc;
    lc.Init(10);
    lc.Display();
}

int main()
{
    Fun();
    //LocalClass lc; error 局部类只能在函数体中使用
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值