C++期中总结笔记
1.C++不仅支持面向对象,也可以像C一样支持面向过程.
2.OOP三大特性:封装 继承 多态
3.函数重载依据:函数类型and形参个数,返回类型不能作为依据
4.常成员函数是指通过函数获得成员的值,不一定用const修饰返回值.
5.引用就是取别名
6.名称空间防止命名冲突
7.private修饰函数和成员
8.inline内联函数
9.构造函数只负责初始化,不负责分配对象占用内存空间
10.构造函数能够重载,析构函数没有形参.
11.所有成员默认访问属性 private
12.引用类的目的:提供一个机制,实现从现实世界到程序世界的映射 提供代码重用性 数据封装
13.定义类 访问属性: 定义结束;
14.指针无论类型4字节
举个栗子: dataSetMonth(9);<=>SetMonth(&date,9); 返回对象类本身 return *this; 当参数与成员名相同时,不能用n=n,需用this->n=n; this指针实例: class Point { int x,y; public: Point(int a,int b) { x=a; y=b; } void MovePoint(int a,int b) { x+=a; y+=b; } void print() { cout<<"x="<<endl; } }; int main() { Point point1(10,10); point1.MovePoint(2,2); point1.print(); } MovePoint函数中,x+=a <=> this->x+=a;x+=b <=> this->x+=b;
举个栗子 class A { public: int func(int p) { } };
举个栗子 #include <iostream> using namespace std; class CExample { private: int a; public: CExample(int b) { a=b; } CExample(const CExample& C) { a=C.a; } void show() { cout<<a<<endl; } }; int main() { CExample A(100); CExample B=A; B.show(); return 0; }
#include <cmath> #include "circle.h" Circle::Circle(double aX,double aY,double aR):m_iX(aX),m_iY(aY),m_iRadius(aR){} Circle::~Circle(){} void Circle::setPos(double aX,double aY) { m_iX=aX; m_iY=aY; } void Circle::getPos(double &rX,double &rY)const { rX=m_iX; rY=m_iY; } double Circle::getRadius()const { return m_iRadius; } double Circle::getDist(const Circle & aRef)const { double x1,y1,x2,y2; this->aRef.getPos(x1,y1); double r=pow(x1-x2,2); r += pow(y1-y2,2); return sqrt(r); } bool Circle::isInter(const Circle & aRef)const { double min_dist=this->getRadius()+aRef.getRadius(); double dist =this->getDist(aRef); if(min_dist>=dist) { return true; } return false; }
#ifndef ARRAY_H #define ARRAY_H #include <iostream> class Array { public: Array(const int aCount=10); Array(const Array &aArray); ~Array(); void setCount(const int aCount); int getValue(const int aIndex)const; void setValue(const int aIndex,const int aValue); private: int m_iCount,*m_pArray; }; #endif Array:Array(const int aCount):m_iCount(aCount),m_pArray(NULL) { if(aCount<=0) { return false; } m_pArray=new int[aCount]; } Array:Array(const Array &aArray):m_iCount(aArray.m_iCount),m_pArray(NULL) { if(aArray.m_iCount<=0) { return false; } this->m_pArray=new int[aArray.m_iCount]; for(int I=0;i<m_iCount;i++) { m_pArray[i]=aArray.m_pArray[i]; } } Array::~aArray() { delete []m_pArray; } void Array::setCount(const int aCount) { if(!aCount) { return false; } int min_count=(aCount<m_iCount)?aCount:m_iCount; m_iCount=aCount; int *nArray=new int[aCount]; if(min_count) { memcpy(nArray,m_pArray,min_count*sizeof(int)); } delete []m_pArray; m_pArray=nArray; } int Array::getValue(const int aIndex)const { return m_pArray[aIndex]; } void Array::setValue(const int aIndex,const int aValue) { if(!m_pArray) { return false; } if(aIndex>=m_iCount) { return false; } m_pArray[aIndex]=aValue; }
Complex::Complex(double aReal,double aImag) { m_dImag=aImag; m_dReal=aReal; } Complex::~Complex(){} double Complex::GetReal()const { return m_dReal; } double Complex::GetImag()const { return m_dImag; } Complex Complex::Add(Complex &aRef)const { Complex result; result.m_dImag=m_dImag+aRef.m_dImag; result.m_dReal=m_dReal+aRef.m_dReal; return result; }
#include <iostream> using namespace std; class String{ public: String(const char* str=NULL); String(const String &other); ~String() { delete []m_data; } void display()const; private: char *m_data; }; String::String(const char* str) { if(!str) { m_data=0; } else { m_data=new char[strlen(str)+1]; strcpy(m_data,str); } } String::String(const String & other) { if(this!=&other) { m_data=new char[strlen(other.m_data)+1]; strcpy(m_data,other.m_data); } } void String::display()const { cout<<m_data<<endl; }
举个栗子 vector<int> vi; //无元素向量对象 string s; //空字符串 Sales_item item; //空的Sales_item对象