2016年06月16日学习日记 c++第二讲

2016年06月16日学习日记 c++第二讲
1、函数重载(overload):在同一个作用域中,函数名相同,参数列表不同(参数个数不同,参数类型不同)
eg:

void func(int n1){}
void func(float n1){}
void func(int n1,int n2){}

如果构造函数有默认值,创建对象不带参数,形參使用默认值,带有参数->形參使用参数值
eg:
构造函数:

Circle(_x=100,_y=200)
{
x=_x;
y=_y;
}

1)带参数

Circle c(1,1);
c.print();//1 1

2)不带参数

Circle c();
c.print();//100 200

为了避免歧义,类中最多只能出现一个默认构造函数,不要让构造函数产生二义性(创建对象时,可以选择执行两个或者更多构造函数。比如又有无參构造函数,又有全带默认值的构造函数,因为函数原型相同!)

初始化列表:初始化列表的作用与构造函数体内的作用相同,都是用于初始化成员变量。
用’:’开始初始化 括号外面是要被初始化的属性,括号里面是初始化的值,每个属性的初始化用逗号隔开。
1、初始化列表只能使用在构造函数中
2、初始化列表形式比在函数体中赋值形式效率高

Circle::Circle():x(0),y(0){
//x(0) int x = 0 初始化列表提高了效率
//x = 0; int x; x = 0;
}
Circle::Circle(int _x,int _y):x(_x),y(_y){
}

4种情况必须使用初始化列表:
1)const属性 2)引用属性 3)组合 4)继承
组合(复合):一个类的成员变量事另外一个类的对象,那么前者被称为复合类
注意:组合类中国年的构造函数调用(先调用属性对象的构造函数,再初始化另外属性部分)

Circle::Circle:pc(),radius(0){}
Circle::Circle(int _x,int _y,int _radius): pc(_x,_y),radius(_radius){}

课堂练习
一个点类:
属性:x,y
行为:构造函数,输出函数
CPoint.hpp

#ifndef CPoint_hpp
#define CPoint_hpp

#include <stdio.h>
class CPoint
{
private:
    float x;
    float y;
public:
    float getX();
    float getY();
    CPoint();
    CPoint(int _x,int _y);
    void Print();
    float dis(CPoint p);
};
#endif
CPoint.cpp
#include "CPoint.hpp"
#include <iostream>
#include <math.h>
using namespace std;
CPoint::CPoint()
{
    x=0;
    y=0;
}
CPoint::CPoint(int _x,int _y)
{
    x=_x;
    y=_y;
}
float CPoint::dis(CPoint p)
{
    return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
}
float CPoint::getX()
{
    return x;
}
float CPoint::getY()
{
    return y;
}
void CPoint::Print()
{
    cout<<"x = "<<x<<" y = "<<y<<endl;
}


一个直线类:
 属性:一个A点,一个B点
 行为:构造函数,输出函数
Cline.hpp
#ifndef CLine_hpp
#define CLine_hpp
#include "CPoint.hpp"
#include <stdio.h>
class CLine 
{
private:
    CPoint p1,p2;
public:
    CLine();
    CLine(CPoint p1,CPoint p2);
    CLine(int x1,int y1,int x2,int y2);
    float dis();
    void Print();
};
#endif

Cline.cpp

#include "CLine.hpp"
#include <cmath>
#include <iostream>
using namespace std;
CLine:: CLine(CPoint p1,CPoint p2){
    this->p1=p1;
    this->p2=p2;
}
CLine::CLine(int x1,int y1,int x2,int y2):p1(x1,y1),p2(x2,y2)
{
}
CLine::CLine()
{
}

float CLine::dis()
{
//    return sqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX())+(p1.getY()-p2.getY())*(p1.getY()-p2.getY()));
    return sqrt(pow((p1.getX()-p2.getX()),2)+pow((p1.getY()-p2.getY()),2));
}
void CLine::Print()
{
    cout<<"A : ("<<p1.getX()<<","<<p1.getY()<<")"<<" B : ("<<p2.getX()<<","<<p2.getY()<<")"<<endl;
    cout<<"length : "<<dis()<<endl;
}

一个平面类:
属性:一个直线,一个高(height)
行为:构造函数,输出函数,计算面积函数,计算周长函数
CPlane.hpp

#ifndef CPlane_hpp
#define CPlane_hpp
#include "CLine.hpp"
#include <stdio.h>
class CPlane
{
private:
    CLine l;
    float height;
public:
    CPlane();
    CPlane(CLine _l,float _height);
    CPlane(CPoint _p1,CPoint _p2,float _height);
    CPlane(float x1,float y1,float x2,float y2,float _height);
    void Print();
    float getArea();
    float getGirth();
};

#endif

CPlane.cpp

#include "CPlane.hpp"
#include <iostream>
using namespace std;
CPlane::CPlane()
{
}
CPlane::CPlane(CLine _l,float _height):l(_l),height(_height)
{
}
CPlane::CPlane(CPoint _p1,CPoint _p2,float _height):l(_p1,_p2),height(_height)
{

}
CPlane::CPlane(float x1,float y1,float x2,float y2,float _height):l(x1,y1,x2,y2),height(_height)
{
}
void CPlane::Print()
{
    l.Print();
    cout<<"height : "<<height<<endl;
    cout<<"area : "<<getArea()<<endl;
    cout<<"girth : "<<getGirth()<<endl;

}
float CPlane::getArea()
{
    return l.dis()*height;
}
float CPlane::getGirth()
{
    return 2*l.dis()+2*height;
}

析构函数:回收空间(回收构造函数时再堆上开辟的空间) [’~’+类名]
对象销毁时自动调用析构函数
作用:当对象生命周期结束时,回收对象所占资源
析构函数的四个特点:
1)没有返回值
2)不能带参数
3)唯一,不能被重载(因为不能带参数)
4)访问权限为公有

堆:
C:malloc free
C++:new delete
1:开辟一个空间

int *p = new int (5);//在堆上开辟一个int型(4个字节)空间,这个空间上存储5这个数字
char *p1 = new char(‘c’);
delet p;delete p1;

2:开辟一片连续空间
char *p2 = new char[10];//在堆上开辟10个char大小(1个字节)的空间

int *p3 = new int[20];
delete [] p2;
delete [] p3;

当属性有指针的时候,指针指向的空间必须在堆上,指针本身还是存在栈上
eg:

Test::Test( int * _p)
{
}

标准构造函数与析构函数
Test.hpp

#ifndef Test_hpp
#define Test_hpp

#include <stdio.h>
class Test
{
private:
    int *p;
public:
    Test(int *_p);
    ~Test();
};
#endif

Test.cpp

#include "Test.hpp"
#include <iostream>
using namespace std;
Test::Test( int * _p)
{
    //p=_p; 不要这样写
    cout<<"构造函数被调用"<<endl;
    p=new int[5];
    for (int i=0;i<5;++i)
    {
        p[i]=_p[i];
        cout<<p[i]<<endl;
    }

}
Test::~Test()
{
    if(p != NULL)
    {
        delete[] p;
        p=NULL;
    }
    cout<<"析构函数被调用"<<endl;
}

下午课堂练习:
1、
Person类:
属性:char *name,char *sex,int age,char *enjoy
行为:构造函数、输出函数、set函数、get函数
CPerson.hpp

#ifndef CPerson_hpp
#define CPerson_hpp

#include <stdio.h>
class CPerson
{
private:
    char *name;
    char *sex;
    int age;
    char *enjoy;
public:
    CPerson ();
    CPerson(char *_name,char *_sex,int _age,char *_enjoy);
    ~CPerson();
    void print();
    void set(char *_name,char *_sex,int _age,char *_enjoy);
    char* getName();
    char* getSex();
    int getAge();
    char* getEnjoy();
};
#endif

CPerson.cpp

#include "CPerson.hpp"
#include <string>
#include <iostream>
using namespace std;
CPerson:: CPerson ()
{

}
CPerson::CPerson(char *_name,char *_sex,int _age,char *_enjoy){
    name=new char(strlen(_name));
    strcpy(name, _name);
    sex=new char(strlen(_sex));
    strcpy(sex, _sex);
    age=_age;
    enjoy=new char(strlen(_enjoy));
    strcpy(enjoy, _enjoy);
}
CPerson::~CPerson()
{
    cout<<"调用析构函数"<<endl;
}
void CPerson::print()
{
    cout<<"name :"<<name<<endl;
    cout<<"sex :"<<sex<<endl;
    cout<<"age :"<<age<<endl;
    cout<<"enjoy :"<<enjoy<<endl;

}

void CPerson::set(char *_name,char *_sex,int _age,char *_enjoy){
    name=new char(strlen(_name)+1);
    strcpy(name, _name);
    sex=new char(strlen(_sex)+1);
    strcpy(sex, _sex);
    age=_age;
    enjoy=new char(strlen(_enjoy)+1);
    strcpy(enjoy, _enjoy);
}

char* CPerson::getName(){
    return name;
}

char* CPerson::getSex(){
    return sex;
}

int CPerson::getAge(){
    return age;
}

char* CPerson::getEnjoy(){
    return enjoy;
}
main.cpp
CPerson cp1("lily","girl",20,"dadoudou");
    cp1.print();

2、
组合:
Date类:
属性:int year,int month,int day;
行为:
Person类:char *name,char *sex,int age,char *enjoy,Date birthday
构造函数,析构函数,输出函数,set get函数
CDate.cpp

#ifndef CDate_hpp
#define CDate_hpp

#include <stdio.h>
class CDate
{
private:
    int year;
    int month;
    int day;
public:
    CDate();
    CDate(int _year,int _month,int _day);
    ~CDate();
    void set(int _year,int _month,int _day);
    int getYear();
    int getMonth();
    int getDay();
    void print();

};
#endif

CDate.cpp

#include "CDate.hpp"
#include <iostream>
using namespace std;
CDate::CDate():year(0),month(0),day(0)
{
    cout<<"调用CDate无參构造函数"<<endl;
}
CDate::CDate(int _year,int _month,int _day):year(_year),month(_month),day(_day)
{
    cout<<"调用CDate有參构造函数"<<endl;
}
CDate::~CDate()
{
    cout<<"调用CDate析构函数"<<endl;
}
void CDate::set(int _year,int _month,int _day)
{
    year=_year;
    month=_month;
    day=_day;
}
int CDate::getYear()
{
    return year;
}

int CDate::getMonth()
{
    return month;
}

int CDate::getDay()
{
    return day;
}

void CDate::print()
{
    cout<<"year :"<<year<<endl;
    cout<<"month :"<<month<<endl;
    cout<<"day :"<<day<<endl;
}

CPerson.hpp

#ifndef CPerson_hpp
#define CPerson_hpp
#include "CDate.hpp"
#include <stdio.h>
class CPerson
{
private:
    char *name;
    char *sex;
    int age;
    char *enjoy;
    CDate birthday;
public:
    CPerson ();
    CPerson(char *_name,char *_sex,int _age,char *_enjoy,CDate _birthday);
    CPerson(char *_name,char *_sex,int _age,char *_enjoy,int _year,int _month,int _day);
    ~CPerson();
    void print();
    void set(char *_name,char *_sex,int _age,char *_enjoy);
    char* getName();
    char* getSex();
    int getAge();
    char* getEnjoy();
};
#endif

CPerson.cpp

#include "CPerson.hpp"
#include <string>
#include <iostream>
using namespace std;
CPerson:: CPerson ()
{
    cout<<"调用CPerson无參析构函数"<<endl;
}
CPerson::CPerson(char *_name,char *_sex,int _age,char *_enjoy,CDate _birthday):age(_age),birthday(_birthday)
{
    name=new char(strlen(_name));
    strcpy(name, _name);
    sex=new char(strlen(_sex));
    strcpy(sex, _sex);
    age=_age;
    enjoy=new char(strlen(_enjoy));
    strcpy(enjoy, _enjoy);
    cout<<"调用CPerson参数有CDate对象的构造函数"<<endl;
}
CPerson::CPerson(char *_name,char *_sex,int _age,char *_enjoy,int _year,int _month,int _day):age(_age),birthday(_year,_month,_day)
{
    name=new char(strlen(_name));
    strcpy(name, _name);
    sex=new char(strlen(_sex));
    strcpy(sex, _sex);
    enjoy=new char(strlen(_enjoy));
    strcpy(enjoy, _enjoy);
    cout<<"调用CPerson输入年月日的构造函数"<<endl;

}

CPerson::~CPerson()
{
    cout<<"调用CPerson析构函数"<<endl;
}
void CPerson::print()
{
    cout<<"name :"<<name<<endl;
    cout<<"sex :"<<sex<<endl;
    cout<<"age :"<<age<<endl;
    cout<<"enjoy :"<<enjoy<<endl;
    birthday.print();

}

void CPerson::set(char *_name,char *_sex,int _age,char *_enjoy){
    name=new char(strlen(_name)+1);
    strcpy(name, _name);
    sex=new char(strlen(_sex)+1);
    strcpy(sex, _sex);
    age=_age;
    enjoy=new char(strlen(_enjoy)+1);
    strcpy(enjoy, _enjoy);
}

char* CPerson::getName(){
    return name;
}

char* CPerson::getSex(){
    return sex;
}

int CPerson::getAge(){
    return age;
}

char* CPerson::getEnjoy(){
    return enjoy;
}

3、一个集合类set:
Person* array[20]; 存放person
int size 标记数组中有多少元素
构造函数(初始化size=0),析构函数,Add函数(addPerson(Person* p))->把p添加到array中

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值