cpp学习笔记(二)

cpp学习笔记(二)

作为一个cpp的初学者,我一直在语法的泥潭里面挣扎。
我想不要太在意那些语法细节,不对语法做深刻的思考和总结,快速的对cpp的语法有一个大致的了解

对应的章节

10 类和对象
11 实现继承
12 多态
13 运算符类型和运算符重载
14 类型转换运算符
15 宏和模板

第10章

什么是类和对象
如何定义新类并创建其对象
什么是成员函数和成员数据
什么是构造函数?如何使用它们

第11章

什么是继承
如何使用继承从一个类中派生出另外一个类
什么是protected,如何使用它
什么是虚方法
什么是私有继承
有继承(public)

第12章

什么是多继承以及如何使用它
什么是虚继承以及如何使用它
什么是抽象类以及如何使用它
什么是纯虚函数

第13章

使用关键字operator
单目运算符
    ++ -- * -》 ! & ~ + - 转换运算符
    mark 智能指针
双目运算符
 , != > < =


转换运算符
不能重新定义的运算符 . .* ::  ?: sizeof

第14章

为何需要类型转换运算符
为什么有些c++程序员不喜欢传统c风格类型转换
4个c++类型转换运算符
为什么c++类型转换运算符并非总是最佳选择

第15章

预处理器简介
关键字define与宏
模板简介
编写函数模板和类模板
宏和模板之间的区别

main函数

//
//  main.cpp
//  object_9_15
//
//  Created by bikang on 16/9/26.
//  Copyright (c) 2016年 bikang. All rights reserved.
//

#include <iostream>
#include "Cat.h"
#include "Animal.h"
#include "Pegasus.h"
#include "OperateNew.h"
using namespace std;

void t10Class();//test object
void t11classInherit();//test inherit
void t12classInherit();//test more inherit
void t13operator();
void t14operator();//static_cast dynamic_cast reinterpret_cast const_cast
void t15operator();

inline unsigned long squareData(unsigned long a){return a*a;}
#define MAX(X,Y) (X)>(Y)?(X):(Y)

//模板

template <typename objectName>
objectName& getMin(objectName &a, objectName &b) {
    if(a > b) return b;
    else{
        return a;
    }
}

template <typename T=int>
class TemplateClass{
public:
    void setValue(T& newValue){
        m_value = newValue;
    }
    T& getValue(){
        return m_value;
    }
private:
    T m_value;
};


int main(int argc, const char * argv[]) {
    //t10Class();
    //t11classInherit();
    //t12classInherit();
    //t13operator();
    //t14operator();
    t15operator();
    return 0;
}

void t15operator(){
    //宏
    int max = MAX(11,2);
    cout << max << endl;
    //内联方法
    unsigned long long1 = squareData(11);
    cout << long1<<endl;
    //模板方法
    int a1 = 11,a2 = 12;
    int min = getMin<int>(a1, a2);
    cout << min <<endl;
    //模板类
    TemplateClass <> tclass0;
    tclass0.setValue(a1);
    cout << tclass0.getValue() << endl;

    TemplateClass <int> tclass1;
    tclass1.setValue(a1);
    cout << tclass1.getValue() << endl;

    TemplateClass<string> tclass2;
    string s1 = "c++初学者";
    tclass2.setValue(s1);
    cout << tclass2.getValue()<<endl;

    return;
}


void t14operator(){


    Animal *animal1 = new Animal();
    Dog *dog1 = new Dog();


    //static_cast 显示执行隐式的转换,不会再运行阶段检查
    double double1 = 12.12;
    int num = static_cast<int>(double1);
    cout << num << endl;
    Animal *animal2 = static_cast<Animal *>(dog1);
    Dog *dog2 = static_cast<Dog *>(animal1);
    delete dog2;
    delete animal2;
    //dynamic_cast 运行阶段类型识别

    //Dog *dog3 = dynamic_cast<Dog *>(animal1);
    //error
    /*
    if(dog3){
        cout << "cast right";
    }*/
    //delete dog3;

    //reinterpret_cast
    Dog *dog4 = reinterpret_cast<Dog*>(animal1);
    if(dog4) cout << "cast right" <<endl;

    //const_cast 去除静态转换

    const int a = 12;

    int &b = const_cast<int&>(a);
    b = 13;
    cout << "a="<< a << " b="<<b<<endl;



}


void t13operator(){
    OpetateNew op1(2016,9);
    op1.showData();
    //前缀
    ++op1;
    op1.showData();
    //后缀
    OpetateNew op2 = op1++;
    op2.showData();

    int a = op2;
    cout << a<<endl;

    OpetateNew op3(op2+3);
    op3.showData();

    OpetateNew op4(2018,9);
    op1.showData();

    bool a2 = (op4 == op2);
    cout << a2<< endl;

    cout << op2[22] <<endl;
    op2("end&&&");

}
// test more inherit
void t12classInherit(){
    Pegasus p;
    p.chirp();
}
//test inherit
void t11classInherit(){
    /*
    Animal a3(1,2);
    Animal* a4 = a3.clone();
    a4->speek();*/

    //虚方法,虚方法的原理是增加一个虚指针
    Animal *a2 = new Dog(2,3,"black");
    a2->speek();
    Animal *a5 = a2->clone();
    delete a5;
    delete a2;

    return;
}
void tdog(){
    //拷贝
    Animal ani3(2,3);
    ani3.speek();
    Animal ani4 = ani3;

    Dog dog(11,11,"white");
    dog.speek();
    //调用基类的方法
    dog.Animal::speek();
    Animal ani(2,3);
    ani.speek();
    std::cout << "#########";
}

void t10Class(){
    cout << "##################"<<endl;
    Cat cat;
    cat.setAge(10);
    cat.setWeight(10);
    cat.showCat();

    cout << "##################"<<endl;
    Cat cat2(11,11.1,22.1,22.2);
    cat2.showCat();
    cat2.showPosition();
}

测试继承

//
//  Animal.h
//  object_9_15
//
//  Created by bikang on 16/9/28.
//  Copyright (c) 2016年 bikang. All rights reserved.
//

#ifndef __object_9_15__Animal__
#define __object_9_15__Animal__

#include <iostream>



class Animal{
public:
    Animal();
    Animal(unsigned int a,float w);
    Animal(const Animal &animal);
    virtual Animal* clone(){return new Animal(*this);}
    //~Animal();
    virtual ~Animal();
    void setAge(unsigned int a);
    void setWeight(float w);
    unsigned int getAge() const;
    float getWeight() const;
    //void speek();
    virtual void speek();
protected:
    unsigned int age;
    float weight;

};

class Dog:public Animal{
public:
    Dog();
    Dog(unsigned int a,float w,std::string c);
    ~Dog();
    virtual Animal* clone(){return new Dog(*this);}
    void speek();
    void setColor(std::string c);
    std::string getColor() const;
private:
    std::string color;
};

#endif /* defined(__object_9_15__Animal__) */
//
//  Animal.cpp
//  object_9_15
//
//  Created by bikang on 16/9/28.
//  Copyright (c) 2016年 bikang. All rights reserved.
//

#include <iostream>
#include "Animal.h"



Animal::Animal(){
    std::cout << "animal construct \n";
}
Animal::Animal(unsigned int a,float w):age(a),weight(w){
    std::cout <<"animal construct2 \n";
}
//赋值函数
Animal::Animal(const Animal &animal){

   std::cout <<"animal copy \n";
}

Animal::~Animal(){
    std::cout << "animal destruct \n";
}

void Animal::setAge(unsigned int a){
    age = a;
}
void Animal::setWeight(float w){
    weight = w;
}
unsigned int Animal::getAge() const{
    return age;
}
float Animal::getWeight() const{
    return weight;
}

void Animal::speek(){
    std::cout<< "Animal speek \n";
}

Dog::Dog(){
    std::cout << "dog construct \n";
}
Dog::Dog(unsigned int a,float w,std::string c):Animal(a,w),color(c){

}
Dog::~Dog(){
    std::cout << "dog destruct \n";
}
void Dog::speek(){
   std::cout <<"age="<< age << ",weight="<<weight<<",color="<<color<<"\n";
}
//
//  Cat.h
//  object_9_15
//
//  Created by bikang on 16/9/28.
//  Copyright (c) 2016年 bikang. All rights reserved.
//

#ifndef __object_9_15__Cat__
#define __object_9_15__Cat__

class Position{
public:
    Position();
    Position(float x,float y);
    ~Position();
    float getX() const;
    float getY() const;
private:
    float x;
    float y;
};

class Cat{
public:
    Cat();
    Cat(int a, float w);
    Cat(int a, float w,float x,float y);
    ~Cat();
    void setAge(unsigned int a);
    void setWeight(float w);
    unsigned int getAge() const;
    float getWeight() const;
    void showCat();
    Position getPosition() const;
    void showPosition();
private:
    Position position;
    unsigned int age;
    float weight;
};


#endif /* defined(__object_9_15__Cat__) */
//
//  Cat.cpp
//  object_9_15
//
//  Created by bikang on 16/9/28.
//  Copyright (c) 2016年 bikang. All rights reserved.
//

#include <iostream>
#include "Cat.h"

Position::Position(){
    std::cout << "Position construct\n";
}
Position::Position(float x,float y):x(x),y(y){
    std::cout << "Position construct2 " << x <<","<< y << "\n" ;
}
Position::~Position(){
    std::cout << "Position destruct\n";
}

float Position::getX() const{
    return x;
}
float Position::getY() const{
    return y;
}

Cat::Cat(){
    std::cout << "construct\n";
}

Cat::Cat(int a,float w ):age(a),weight(w){
    std::cout << "construct2\n";
}

Cat::Cat(int a, float w,float x,float y):age(a),weight(w),position(x,y){
    std::cout << "construct2\n";
}

Cat::~Cat(){
    std::cout << "destruct\n";
}

void Cat::setAge(unsigned int a){
    age = a;
}
void Cat::setWeight(float w){
    weight = w;
}
unsigned int Cat::getAge() const{
    return age;
}
float Cat::getWeight() const{
    return weight;
}
Position Cat::getPosition() const{
    return position;
}

void Cat::showPosition(){
    float x = position.getX();
    float y = position.getY();
    std::cout << "cat in "<<  x << "," << y << "\n";
}

void Cat::showCat(){
    std::cout << "age="<<age<<"\tweight="<<weight<<std::endl;
}

测试多态和抽象类

//
//  Pegasus.h
//  object_9_15
//
//  Created by bikang on 16/9/28.
//  Copyright (c) 2016年 bikang. All rights reserved.
//

#ifndef __object_9_15__Pegasus__
#define __object_9_15__Pegasus__

#include <iostream>

class Animal2{
public:
    Animal2(){std::cout << "Animal2 construct\n";}
    virtual ~Animal2(){std::cout << "Animal2 destruct\n";}
    virtual void speek() = 0;

};


class Bird:virtual public Animal2{
public:
    Bird(){std::cout << "Bird construct\n";}
    virtual ~Bird(){std::cout << "Bird destruct\n";}
    virtual void fly() const{
        std::cout << "fly fly!!\n";
    }
    void speek(){
        std::cout << "Bird speak\n";
    }

private:
    int age;
};
class Horse:virtual public Animal2{
public:
    Horse(){std::cout << "Horse construct\n";}
    virtual ~Horse(){std::cout << "Horse destruct\n";}
    virtual void whinny() const{
        std::cout << "whinny\n";
    }
    void speek(){
        std::cout << "Horse speak\n";
    }
private:
    int weight;
};

class Pegasus:public Bird,public Horse{
public:
    Pegasus(){std::cout << "Pegasus construct\n";}
    ~Pegasus(){std::cout << "Pegasus destruct\n";}
    void chirp() const{ whinny();}
    void speek(){
        std::cout << "Pegasus speak\n";
    }
};



#endif /* defined(__object_9_15__Pegasus__) */
//
//  Pegasus.cpp
//  object_9_15
//
//  Created by bikang on 16/9/28.
//  Copyright (c) 2016年 bikang. All rights reserved.
//

#include "Pegasus.h"

测试重载

//
//  OperateNew.h
//  object_9_15
//
//  Created by bikang on 16/9/28.
//  Copyright (c) 2016年 bikang. All rights reserved.
//

#ifndef __object_9_15__OperateNew__
#define __object_9_15__OperateNew__

#include <iostream>

class OpetateNew{
public:
    OpetateNew(){

    }
    OpetateNew(int y,int m):year(y),month(m){

    }

    OpetateNew& operator ++ (){
        year++;
        month++;

        return *this;
    }

    operator int(){
        return year+month;
    }

    OpetateNew operator +(int y){
        OpetateNew newObj(year,month);
        newObj.year = newObj.year+y;
        return newObj;
    }

    bool operator == (const OpetateNew &obj){
        //return (obj.year == year) && (obj.month == month);
        return (obj.year == year);
    }

    int& operator [](int index){
        return year;
    }

    void operator ()(std::string inStr){
        std::cout << inStr << std::endl;
    }


    OpetateNew operator ++ (int){
        OpetateNew newObj(year,month);
        newObj.year++;
        newObj.month++;
        return newObj;
    }
    void showData() const{
        std::cout << "year="<<year<<" month"<<month<<"\n";
    }
    ~OpetateNew(){
    }

private:
    int year,month;
};




#endif /* defined(__object_9_15__OperateNew__) */
//
//  OperateNew.cpp
//  object_9_15
//
//  Created by bikang on 16/9/28.
//  Copyright (c) 2016年 bikang. All rights reserved.
//

#include "OperateNew.h"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值