C++ 视频课笔记3

来源:互联网 发布:阜阳市工商局网络监管 编辑:程序博客网 时间:2024/06/02 06:25
3.11 常对象成员和常成员函数

常对象成员
class Line
{
public:
Line(int x1,int y1,int x2,int y2);
private:
const Coordinate m_coorA;
const Coordinate m_coorB;
}

初始化: Line::Line(int x1,int y1,int x2,int y2):m_coorA(x1,y1),m_coorB(x2,y2)

常成员函数
class Coordinate
{
public:
Coordinate(int x,int y);
void changeX()const;
void changeX();      互为重载
private:
int m_iX;
int m_iY;
}
常成员函数中不能修改数据成员的值
实例化时:通过const修饰
int main()
{
const Coordinate coordinate(3,5);
coordinate.changeX(); 
return 0;
}


3.13 常指针和常引用(只有只读权限

int main()
{
Coordinate coor1(3,5);
const Coordinate &coor2 = coor1;      常引用
const Coordinate *pCoor2 = &coor1;    常指针
coor1.printlnfo();
coor2.getX();
pCoor->getY();
return 0;
}

单元巩固:定义一个坐标类,在栈上实例化坐标类常对象,并给出坐标(3,5),然后给定义常引用,常指针,最后使用对象,引用,指针分别通过调用信息打印函数打印坐标信息。

#include<iostream>
#include<stdlib.h>
using namespace std;

class coordinate
{
public:
     coordinate(int x, int y)
     {
          m_ix = x;
          m_iy = y;
     }
     ~coordinate(){};
     void printinfo() const
     {
          cout << "(" << m_ix << "," << m_iy << ")" << endl;
     }

public:
     int m_ix;
     int m_iy;
};

int main()
{
     const coordinate coor(3, 5);
     const coordinate *p = &coor;
     const coordinate  &c = coor;;
     coor.printinfo();
     p->printinfo();
     c.printinfo();
     system("pause");
     return 0;
}

4.C++远征--继承篇
4.1继承

定义两个类:
Person.h

#include<iostream>
using namespace std;

class Person
{
public:
     Person();
     ~Person();
     void eat();
     string m_strName;
     int m_iAge;
};

Worker.h

#include "Person.h"

class Worker :public Person
{
public:
     Worker();
     ~Worker();
     void work();
     int m_iSalary;

};
4.1.1继承
公有继承  class A:public B
保护继承 class A:protected B
私有继承 class A:private B

1.公有继承
private 成员                                           无法访问
protected成员         public                      protected
public成员                                              publc

2保护继承
private 成员                                           无法访问
protected成员        private                    private
public成员                                            private

3.私有继承
private 成员                                           无法访问
protected成员         protected                protected
public成员                                             protected
原创粉丝点击