C++笔记(十五)类和对象

//learnc150.h
/*
面向对象编程(OOP)是一种特殊的设计程序的概念方法。OOP特性如下:
1:抽象
2:封装和数据隐藏
3:多态
4:继承
5:代码可重用性
为了实现这些特性,C++提供了类。使用class来声明一个类。类中可以包含属性和方法。
*/
#ifndef LEARNC_15
#define LEARNC_15

#include<string>

class User //声明一个类,名称为User。
{
private://封装私有属性,
    static const int MONTHS = 12;//静态类成员,所有User对象共享。
    std::string name;
    int age;
    int *sex;

public://共有方法;
    User();//默认构造函数
    User(std::string namep,int agep);//构造函数,用来初始化成员变量
    ~User();//析构函数
    void show();//常规成员函数
    void speak() const;//const成员函数
};


//栈的实现类 FILO(先进后出)
class Stack 
{
private:
    enum { MAX = 5 };
    int items[MAX];
    int top;//栈顶下标
public:
    Stack();//默认构造函数
    bool isEmpty();//是否没有元素
    bool isFull();//元素是否满了
    bool push(int item);//压入元素
    bool pop(int &item);//取出元素
};

#endif
//learnc151.cpp
#include<iostream>
#include "learnc150.h"

using std::cout;
using std::endl;

User::User() {
    this->name = "";
    this->age = 0;
    this->sex = new int;
    *sex = 0;
}

User::User(std::string namep, int agep) {
    //this指针,表示当前对象
    this->name = namep;  
    this->age = agep;
    this->sex = new int;
    *sex = 1;
}

User::~User() {//对象过期时会调用此方法,用来释放内存
    //释放new的内存
    delete this->sex;
}

void User::speak() const {
    cout << "I can speak chiniese!" << endl;
}

void User::show() {
    cout << "name:" << this->name << " age:" << this->age  << " sex:"<<  *sex <<  endl;
}


Stack::Stack() {
    this->top = 0;
}

bool Stack::isEmpty() {
    return this->top == 0;
}

bool Stack::isFull() {
    return this->top == MAX;
}

bool Stack::push(int item) {
    if (!this->isFull()) {
        cout << "在位置" << this->top << "放入元素" << item << endl;
        this->items[this->top++] = item;
        return true;
    }else{
        cout << "栈中的元素已经满了" << endl;
        return false;
    }
}

bool Stack::pop(int &item) {
    if (this->isEmpty()) {
        cout << "栈中没有元素" << endl;
        return false;
    }else {
        cout << "返回位置" << this->top-1 << "的元素" << this->items[this->top-1] << endl;
        item = this->items[--this->top];
        return true;
    }
}


//learnc152.cpp
#include<iostream>
#include "learnc150.h"

int main() {
    using std::cout;
    using std::endl;


    User u = User("yueer", 2);//或者User u("yueer", 2)
    u.show();
    u.speak();

    const User u1 = User("panpan", 2);
    //u1.show(); const对象 不能调用非const成员函数
    u1.speak();


    //对象数组
    User users[2];//使用默认构造函数初始化
    users[0].speak();
    users[1].speak();

    Stack stack;

    cout << "isEmpty:" << stack.isEmpty() << " isFull:" << stack.isFull() <<  endl;
    stack.push(1);
    cout << "isEmpty:" << stack.isEmpty() << " isFull:" << stack.isFull() << endl;
    stack.push(2);
    cout << "isEmpty:" << stack.isEmpty() << " isFull:" << stack.isFull() << endl;
    stack.push(3);
    cout << "isEmpty:" << stack.isEmpty() << " isFull:" << stack.isFull() << endl;
    stack.push(4);
    cout << "isEmpty:" << stack.isEmpty() << " isFull:" << stack.isFull() << endl;
    stack.push(5);
    cout << "isEmpty:" << stack.isEmpty() << " isFull:" << stack.isFull() << endl;
    stack.push(6);

    int a;
    stack.pop(a);
    cout << a << endl;
    stack.pop(a);
    cout << a << endl;
    stack.pop(a);
    cout << a << endl;
    stack.pop(a);
    cout << a << endl;
    stack.pop(a);
    cout << a << endl;
    stack.pop(a);





    return 0;
}

输出结果:

name:yueer age:2 sex:1
I can speak chiniese!
I can speak chiniese!
I can speak chiniese!
I can speak chiniese!
isEmpty:1 isFull:0
在位置0放入元素1
isEmpty:0 isFull:0
在位置1放入元素2
isEmpty:0 isFull:0
在位置2放入元素3
isEmpty:0 isFull:0
在位置3放入元素4
isEmpty:0 isFull:0
在位置4放入元素5
isEmpty:0 isFull:1
栈中的元素已经满了
返回位置4的元素5
5
返回位置3的元素4
4
返回位置2的元素3
3
返回位置1的元素2
2
返回位置0的元素1
1
栈中没有元素
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值