C++基础(类和构造函数)

C++基础

类和构造函数

  • 类:类是定义同一类所有对象得变量和方法的蓝图或原型。数据与算法的统一。

    • 成员
    • 成员函数
    • 保护数据
    • 封装
  • 类和对象就像人类与人的关系

  • 对象的初始化

    • 建立对象,须有一个有意义的初始值
    • C++建立和初始化对象的过程专门由该类的构造函数来完成。
    • 构造函数给对象分配空间和初始化。
    • 如果一个类没有专门定义构造函数,那么C++就仅仅创建对象而不做任何初始化
#include <iostream>
using namespace std;
class Person
{
    private:
        int id;
        int money;
    public:
        void show()
        {
            cout<<"id="<<id<<"money="<<money<<endl;
        }
        void set(int a,int b)
        {
            if(a<0)
                a=0;
            id=a;
            money=b;
        }
};
int main()
{
    Person a;
    Person b;
    a.set(1,100);
    a.show();
    b.set(2,300);
    b.show();
}
class Person
{
    private:
        int id;
        int money;
    public:
        void show();
        void set(int a,int b);
};
void Person::show()
{
    cout<<"id="<<id<<"money="<<money<<endl;
}
void Person::set(int a,int b)
{
    if(a<0)
        a=0;
    id=a;
    money=b;
}
  • 最终版
//main.cpp
#include "person.h"
int main()
{
    Person a;
    Person b;
    a.set(1,100);
    a.show();
    b.set(2,300);
    b.show();
}
//person.cpp
#include <iostream>
#include "person.h"
using namespace std;
void Person::show()
{
    cout<<"id="<<id<<"money="<<money<<endl;
}
void Person::set(int a,int b)
{
    if(a<0)
        a=0;
    id=a;
    money=b;
}
//person.h
#include "person.h"
class Person
{
    private:
        int id;
        int money;
    public:
        void show();
        void set(int a,int b);
};
  • class默认私有。

  • 面向对象

    • 在面向对象中,算法与数据结构被捆绑为一类
  • 定义成员函数

    • 类中定义成员函数一般为头连函数,即没有明确用。。。。。。。。。
  • 保护成员

    • 保护成员
      • 除了友元和子类的成员函数之外,从类的外部就不能对它们访问
    • 私有成员
      • 从类的外部就不能对它们访问
    • 公共成员
      • 公共成员在任何地方都可以被访问。
  • 不同域对应不同的函数内容

#include <iostream>
using namespace std;        
class person
{
    private:
        int id;
        int money;
    public:
        void show();
        void show(int a);
};
class test
{
    public:
        void show();
};
void person::show()
{
    cout<<"hello1"<<endl;
}
void person::show(int a)
{
    cout<<"hello2"<<endl;
}
void test::show()
{
    cout<<"hello3"<<endl;
}
void show()
{
    cout<<"hello4"<<endl;
}
int main()
{
    person a;
    test b;
    a.show();
    a.show(1);
    show();
    b.show();
}
  • 对象与域的调用方法
#include <iostream>
using namespace std;
class test
{
    public:
        void show()
        {
            cout<<"hello"<<endl;
        }
};
void lianxi1(test *p1)
{
    p1->show();
}
void lianxi2(test &p2)
{
    p2.show();
}
void lianxi3(test p3)
{
    p3.show();
}
int main()
{
    test t;
    lianxi1(&t);
    lianxi2(t);
    lianxi3(t);
    cout<<sizeof(test)<<endl;
}
#include <iostream>
using namespace std;        
class test
{
    private:
        int id;
    public:
        void setid(int i)
        {
            id=i;
        }
        void show()
        {
            cout<<"this"<<(this)<<endl;
            cout<<"id= "<<(id)<<endl;
        }       
};
int main()
{
    test t;
    t.setid(222);
    t.show();
    cout<<"&t="<<&t<<endl;

}
  • 类和struct的区别
    • 除关键字不同外(class,struct)的唯一区别是,结构在默认情况下的成员是公共的,而类在默认情况下的成员是私有的。 在C++中,结构是特殊的类。
  • 类的作用域
    • 一个类的所有成员位于这个类的作用域内
    • 类作用域是类定义和相应类成员定义范围
  • 对象和类的联系与区别

  • 类中访问控制的关键字有哪几个,分别是什么意义

  • 封装理念
    • 封装来源于信息隐藏的设计理念, 是通过特性和行为的组合来创建新数据类型让接口与具体实现相隔离。
    • C++中是通过类来实现的, 为了尽量避免某个模块的行为干扰同一系统中的其它模块,应该让模块仅仅公开必须让外界知道的接口。
  • 封装的优点
    • 重用
    • 不必关心具体的体现
    • 具有安全性

构造函数

  • 定义
    • C++的构造函数和析构,使类对象能轻松的被赋值与撤销。它们是一种特殊的函数。
    • 构造函数是第一个自动调用的函数
    • 构造函数创建类对象,初始化其成员
    • 析构函数撤销类对象
    • 构造函数和析构函数是类的特殊成员函数
    • 与类同名
    • 析构函数
    • 重载构造函数
    • 默认参数构造函数
    • 对象创建过程
#include<iostream>
using namespace std;
class Fract
{
    int m_m;
    int m_z;
public:
      Fract(int z,int m);
      Fract();
      void set(int z,int m)
      {
        m_m=m;
        m_z=z;
      }//set函数依旧保留,以防后续使用。
      void print()
      {
        reduce();
        cout<<m_z<<"/"<<m_m<<endl;
      }
      void reduce()
      {
        for(int i=m_m;i>0;i--)
        {
            if((m_m%i==0)&&(m_z%i==0))
            {
                m_m/=i;
                m_z/=i;
                break;
            }
        }
      }
};
Fract::Fract(int z,int m)
{
    m_m=m;
    m_z=z;
}
Fract::Fract()
{
    m_m=12;
    m_z=16;
    //-cout<<"hello"<<endl;
}
int main()
{
    Fract a(12,16);
//  a.set(12,16);
    a.print();
    Fract b;//一般不用(),会出错。
//  a.set(12,16);
    b.print();
    //-Fractc[5];会调用5次
}
  • 注意
    • 类名==构造函数名
    • 构造函数没有返回值类型,函数体也不允许返回值,但可以有返回语句"return".
  • 建立对象的同时,自动调用构造函数
#include <iostream>
using namespace std;
class test1
{
    public:
        test1()
        {   
            cout<<"hello test1"<<endl;
        }
        
};
class test2
{
    test1 m_t;
    public:
        test2()
        {
            cout<<"hello test2"<<endl;
        }
};
int main()
{
    test2 t;
}
  • 类中成员变量也是类,调用顺序:
#include <iostream>
using namespace std;
class test1
{
    int m_a;
    public:
        test1(int a)
        {   
            m_a=a;
            cout<<"hello test1"<<m_a<<endl;
        }
        test1(){}
};
class test2
{
    test1 m_t;
    test1 m_s;
    public:
        test2()
        {
            cout<<"hello test2"<<endl;
        }
};
int main()
{
    test2 t;
}
//test2()后调用赋值11出现hello test111+hello test2
//不调用则出现hello test2
#include <iostream>
using namespace std;
class test1
{
    int m_a;
    public:
        test1(int a)//
        {   
            m_a=a;
            cout<<"hello test1"<<m_a<<endl;
        }
        test1(){}
        ~test()
        {
            cout<<"析构测试1"<<endl;
        }
};
class test2
{
    test1 m_t;
    test1 m_s;
    public:
        test2():m_s(222),m_t(333)
        {
            cout<<"hello test2"<<endl;
        }   
        ~test2()
        {
            cout<<"析构测试2"<<endl;
        }
        
};
int main()
{
    test2 t;
    cout<<"+++"<<endl;
}
//hello test1333
//hello test1222
//hello test2
//+++
//析构测试2
//析构测试1222
//析构测试1333
  • 析构函数

    • 在t消亡的时候自动调用,无返回值。
    • 析构函数不允许重载。
    • 析构函数有且只有一个。
    • 析构函数是特殊的类成员函数,不能随意调用
  • 析构函数之防止内存泄漏

#include <iostream>
#include <malloc.h>
using namespace std;
class test
{
    int *p;
    public:
        test()
        {
            p=(int *)malloc(sizeof(int));
            *p=4;
            cout<<"test construct"<<endl;
        }
        ~test()
        {
            free(p);
            cout<<"析构测试"<<endl;
        }
        void print()
        {
            cout<<*p<<endl;
        }
};
int main()
{
    test t;
    t.print();
    cout<<"+++++++"<<endl;
}
//test construct
//4
//++++++
//析构测试
  • 手动创建构造函数是创建一个无名函数

  • C++给构造对象的顺序做了专门的规定

    • 局部和静态对象,以声明顺序构造
    • 静态对象只能被构造一次
    • 所有全局对象都在主函数main()之前被构造
    • 全局对象构造时无特殊顺序
    • 成员以其在类声明的顺序构造
  • 构造函数在分配空间之后被调用

bool型变量

#include <iostream>
using namespace std;
int main()
{
    bool bSex=true;
    cout<<"请输入数字"<<endl;
    cin>>bSex;
    cout<<"我叫SB,是一位"<<(bSex?"丑逼":"帅逼")<<endl;
    cout<<"true="<<true<<endl;
    cout<<"false="<<false<<endl;
}
  • 分配与释放顺序
#include <iostream>
using namespace std;
class test
{
    int m_data;
    public:
        test(int data):m_data(data)
        {
            cout<<"test("<<m_data<<")"<<endl;
        }
        ~test()
        {
            cout<<"~test("<<m_data<<")"<<endl;
        }
};
test t1(10);
void fun()
{
    test t4(40);
    return;
}
void show()
{
    static test t5(50);
    return;
}
int main(int argc,char **argv)
{
    test t2(20);
    test t3(30);
    fun();
    show();
}
test t6(60);
  • 类和构造函数制造时钟
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
class myClock
{
    private:
        int myHour;
        int myMin;
        int mySecond;
    public:
        myClock();
    //  void set(int hour,int min,int second);
        void Time();
        void show();
        void run();
};
myClock::myClock():myHour(23),myMin(59),mySecond(55)
{
}
/*
void myClock::set(int hour,int min,int second)
{
    myHour=hour;
    myMin=min;
    mySecond=second;
}*/
void myClock::Time()
{

        sleep(1);
        mySecond++;
        if(mySecond==60)
        {   
            myMin++;
            if(myMin==60)
            {
                myHour++;
                if(myHour==24)
                {
                    myHour=0;
                }
                myMin=0;
            }
            mySecond=0;
        }
    
}
void myClock::show()
{
    cout<<myHour<<":"<<myMin<<":"<<mySecond<<endl;
}
void myClock::run()
{
    while(1)
    {
        system("clear");
        show();
        Time();
    }
}
int main()
{
    myClock c;
//  c.set(23,59,55);
    c.run();
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,829评论 1 331
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,603评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,846评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,600评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,780评论 3 272
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,695评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,136评论 2 293
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,862评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,453评论 0 229
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,942评论 2 233
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,347评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,790评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,293评论 3 221
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,839评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,448评论 0 181
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,564评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,623评论 2 249

推荐阅读更多精彩内容