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
阅读全文
0 0
- C++ 视频课笔记3
- C语言视频学习笔记
- opencv(C++)视频流读入-学习笔记3
- 看朱老师视频笔记-------C语言
- OC视频笔记-3
- OC视频笔记-3
- C++ 视频课笔记1
- C++ 视频课笔记2
- C++ 视频课笔记4
- C++ 视频课笔记5
- C++ 视频课笔记6
- C++ 视频课笔记7
- C++ 视频课笔记8
- 视频学习第一课笔记
- 视频项目笔记(3)
- 孙鑫VC++视频学习笔记-第3课
- object c内存管理总结(关东升老师视频笔记)
- 视频笔记
- NodeJS运行时抛出: Error: listen EADDRINUSE :::3000
- markdown语法技巧2
- sed 工具基本使用方法
- python异常处理
- ApplicationContext 接口容器和BeanFactory 接口容器
- C++ 视频课笔记3
- mybatis数据库查询
- JDBC三种编译
- 2017.7.26学习总结
- python中的继承
- android值制作简易的闹钟和通知栏信息
- C++ 视频课笔记4
- java Iterator
- HDOJ 5098 Smart Software Installer【拓扑排序】