cppReview
最后发布时间:2020-12-06 20:13:27
浏览量:
namespace scope
#include<iostream>
namespace Yahoo{
char* homePageUrl;
}
namespace Google{
char* homePageUrl;
}
using namespace Google;
using Google::homePageUrl;
int main(){
Yahoo::homePageUrl ="a";
homePageUrl ="b";
return 0;
}
Data declarartion in cpp
#include <iostream>
const int MAX = 500;
enum semester{SUMMER=0,FALL,SPRING};
int main(){
semester x = SUMMER;
return 0;
}
Pointer and Reference
int main(){
int a=5;
int *b=&a; // pointer : the memory address of an object
int &c=a; // reference : an alternative name to an object
return 0;
}
File I/O in c++
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ofstream outFile("my.out",ios::out);
if(!outFile){
cout<<"cannot open my.out"<<endl;
return 1;
}
int n = 50;
outFile<<n<<endl;
}
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream inFile("my.out",ios::in);
if(!inFile){
cout<<"cannot open my.out"<<endl;
return 1;
}
int n;
while (!inFile.eof())
{
inFile >> n;
cout<<n<<endl;
}
}
I/O Operator Overloading
#include <iostream>
#include <fstream>
using namespace std;
class Ferrari{
public:
int W,H,L;
char* model;
/***key code***/
// friend ostream& operator<<(ostream &os, const Ferrari &obj){
// os<<"************"<<endl;
// os<<"Ferrari"<<obj.model<<endl;
// os<<"W"<<obj.W<<endl;
// os<<"H"<<obj.H<<endl;
// os<<"L"<<obj.L<<endl;
// }
};
/***alternative key code***/
ostream& operator<<(ostream &os, const Ferrari &obj){
os<<"************"<<endl;
os<<"Ferrari"<<obj.model<<endl;
os<<"W"<<obj.W<<endl;
os<<"H"<<obj.H<<endl;
os<<"L"<<obj.L<<endl;
}
int main(){
Ferrari myCar;
myCar.model=(char*)"car";
cout<<myCar<<endl;
return 0;
}
Function in C++
inline: compiler replaces each function using its function body.
inline int sum(int a,int b){
return a+b;
}
Parameter Passing
- by Value
- by Pointer
- by reference
Passing const arguments
void func(const dataType& a){}
Function Overloading in C++
define two functions of same function name
int Max(int,int);
int Max(int,int,int);
int Max(float,int);
Polymorphism in C++
相同的函数名、参数类型、返回类型
#include <iostream>
using namespace std;
class Shape{
protected:
int width,height;
public:
Shape(int w=0,int h=0){
width=w;
height=h;
}
/***key code***/
virtual int area()=0;
};
//正方形
class Rectangle:public Shape{
public:
Rectangle(int w,int h):Shape{w,h}{}
int area(){
return width*height;
}
};
//三角形
class Triangle:public Shape{
public:
Triangle(int w,int h):Shape{w,h}{} //构造函数初始化
int area(){
return width*height/2;
}
};
int main(){
Shape * shape = new Rectangle(1,2);
cout<<shape->area()<<endl;
shape = new Triangle(1,2);
cout<<shape->area()<<endl;
return 0;
}
Dynamic Memory Allocation(分配) in c++
malloc、delete、realloc、memset、memcopy
new、delete
int main(){
int* x=(int*)malloc(sizeof(int));
free(x);
int* y = new int;
delete y;
int *data = new int[10];
delete [] data;
return 0;
}
Handling Exception
#include <iostream>
using namespace std;
int DivZero(int a,int b){
if(a<=0||b<<0)
throw "All parameters should be > 0";
return a/b;
}
int main(){
try
{
cout<<DivZero(5,5)<<endl;
}
catch(const char * e){
cout<<"error"<<e<<endl;
}
// catch(const std::exception& e)
// {
// std::cerr << e.what() << '\n';
// }
return 0;
}
Type of Constructors
- Default constructor
Rectangle();
- Augmented constructor
Rectangle(int,int,int,int);
Rectangle::Rectangle(int x,int y,int h,int w){
xLow=x;yLow=y;
height=h;width=w;
}
May use member initialization list(more efficient)
Rectangle::Rectangle(int x,int y,int h,int w):xLow(x),yLow(y),height(h),width(w){}
- Copy constructor
Rectangle(const Rectangle&);
Rectangle::Rectangle(const Rectangle& _src){
xLow=_src.xLow;
yLow=_src.y_Low;
height=_src.height;
width=_src.width;
}
Operator Overloading
#ifndef RECTANGLE_H
#define RECATANGLE_H
class Rectangle{
Rectangle();
~Rectangle();
bool operator==(const Rectanfle&);
Rectagle& operator(const Rectangle&);
}
#endif
Template in C++
A mechanism to parameterize the target data type and instantiate it to a proper(恰当的) data type in compline time.
Function Template
template <class T>
T sum(T* data,const int SIZE){
T sum=0;
for(int i=0;i<SIZE;i++){
sum+=data[i];
}
return sum;
}
Class Template
emplate<class T>
struct Z // template definition
{
void f() {}
void g(); // never defined
};
template struct Z<double>; // explicit(明确的,显式的) instantiation of Z<double>
Z<int> a; // implicit(含蓄的,隐式的) instantiation of Z<int>
Z<char>* p; // nothing is instantiated here
p->f(); // implicit instantiation of Z<char> and Z<char>::f() occurs here.
// Z<char>::g() is never needed and never instantiated: it does not have to be defined
C++ Inheritance
Overloading(重载)相同函数名称 v.s. Overriding(重写)继承中
Standard C++ Library
file,stirng
C++ STL(Standard template library)
algrithms, data structure like vector, list, queues and stacks.