c++模板篇
使用友元函数,使得传入进来的对象去访问他的私有数据成员和成员函数
time.h
#pragma once
#ifndef TIME_H
#define TIME_H
#include"match.h"//match是从哪里来的
#include<iostream>
using namespace std;
class Time
{
friend void Match:: printTime(Time &t);//友元函数
public:
Time(int hour, int min, int sec);
private:
int m_iHour;
int m_iMinute;
int m_iSecond;
};
#endif // !TIME_H
time.cpp
#include"time.h"
Time::Time(int hour, int min, int sec)
{
m_iHour = hour;
m_iMinute = min;
m_iSecond = sec;
}
match.h
#pragma once
#ifndef STUDENT_H
#define STUDENT_H
class Time;//通过time声明
class Match
{
public:
void printTime(Time&t);
};
#endif // !STUDENT_H
match.cpp
#include"match.h"
#include"time.h"
#include<iostream>
using namespace std;
void Match::printTime(Time &t)
{
cout << t.m_iHour << ":" << t.m_iMinute << ":" << t.m_iSecond << endl;
}
demo.cpp
#include<iostream>
#include"stdlib.h"
#include"time.h"
#include"match.h"
using namespace std;
//void printTime(Time &t);
int main(void)
{
Time t(6, 34, 25);
Match m;
m.printTime(t);
//printTime(t);
system("pause");
return 0;
}
//void printTime(Time&t)
//{
// cout << t.m_iHour << ":" << t.m_iMinute << ":" << t.m_iSecond << endl;
//}
友元类
time.h
#pragma once
#ifndef TIME_H
#define TIME_H
#include"match.h"//match是从哪里来的
#include<iostream>
using namespace std;
class Match;
class Time
{
friend Match;//友元类
public:
Time(int hour, int min, int sec);
private:
void printTime();
int m_iHour;
int m_iMinute;
int m_iSecond;
};
#endif // !TIME_H
time.cpp
#include"time.h"
Time::Time(int hour, int min, int sec)
{
m_iHour = hour;
m_iMinute = min;
m_iSecond = sec;
}
void Time::printTime()
{
cout << m_iHour << "时" << m_iMinute << "分" << m_iSecond << "秒" << endl;
}
match.h
#pragma once
#ifndef MATCH_H
#define MATCH_H
#include"time.h"
class Match
{
public:
Match(int hour, int min, int sec);
void testTime();
private:
Time m_tTimer;
};
#endif // !MATCH_H
match.cpp
#include"match.h"
#include"time.h"
#include<iostream>
using namespace std;
Match::Match(int hour,int min,int sec):m_tTimer(hour,min,sec)
{
}
void Match::testTime()
{
m_tTimer.printTime();
cout << m_tTimer.m_iHour<< ":" << m_tTimer.m_iMinute << ":" <<m_tTimer.m_iHour << endl;
}
demo.cpp
#include<iostream>
#include"stdlib.h"
#include"time.h"
#include"match.h"
using namespace std;
int main(void)
{
Match m(6, 30, 56);
m.testTime();
system("pause");
return 0;
}
静态数据成员&静态成员函数
Tank.h
#pragma once
#ifndef TANK_H
#define TANK_H
class Tank
{
public:
Tank(char code);
~Tank();
void fire();//普通成员函数
static int getCount();//静态成员函数
private:
static int s_iCount;
char m_cCode;
};
#endif // ! TANK_H
Tank.cpp
#include<iostream>
#include"Tank.h"
using namespace std;
int Tank::s_iCount = 10;//在类的定义之前就初始化
Tank::Tank(char code)
{
m_cCode = code;
s_iCount++;
cout << "Tank" << endl;
}
Tank::~Tank()
{
s_iCount--;
cout << "~Tank" << endl;
}
void Tank::fire()
{
cout << "Tank -- fire" << endl;
}
int Tank::getCount()
{
return s_iCount;
}
demo.cpp
#include"Tank.h"
#include<stdlib.h>
#include<iostream>
using namespace std;
int main(void)
{
Tank *p=new Tank('A');
cout << Tank::getCount() << endl;
Tank *q = new Tank('B');
cout << q->getCount() << endl;
delete p;
delete q;
cout << Tank::getCount() << endl;
//cout << t1.getCount() << endl;//cout << t1.getCount() << endl;
system("pause");
return 0;
}
一元运算符的重载
Coordinate.h
#pragma once
#ifndef COORDINATE_H
#define COORDINATE_H
#include<iostream>
using namespace std;
class Coordinate
{
friend Coordinate&operator-(Coordinate &c);
public:
Coordinate(int x, int y);
Coordinate&operator ++();
Coordinate operator++(int);
//Coordinate &operator-();
int getX();
int getY();
private:
int m_iX;
int m_iY;
};
#endif // !COORDINATE_H
Coordinate.cpp
#include"Coordinate.h"
Coordinate::Coordinate(int x, int y)
{
m_iX = x;
m_iY = y;
}
int Coordinate::getX()
{
return m_iX;
}
int Coordinate::getY()
{
return m_iY;
}
//Coordinate&Coordinate::operator-()
//{
// this->m_iX = -(this->m_iX);
// m_iY = -m_iY;
// return *this;
//
//}
Coordinate&Coordinate::operator++()
{
m_iX++;//++m_iX;
m_iY++;//++m_iY;
return *this;
}
Coordinate Coordinate:: operator++(int)
{
Coordinate old(*this);
this->m_iX++;
this->m_iY++;
return old;
}
Coordinate&operator-(Coordinate&c)
{
c.m_iX = -c.m_iX;
c.m_iY = -c.m_iY;
return c;
}
demo.cpp
#include"Coordinate.h"
#include<iostream>
using namespace std;
int main(void)
{
Coordinate coor1(1, 3);
cout << coor1.getX() << "," << coor1.getY() << endl;
cout << (coor1++).getX() << ",";
cout<< (coor1++).getY() << endl;
cout << coor1.getX() << "," << coor1.getY() << endl;
//-coor1;//coor1.operator-();
//cout << coor1.getX() << "," << coor1.getY() << endl;
system("pause");
return 0;
}
二元运算符的重载
Coordinate.h
#pragma once
#ifndef COORDINATE_H
#define COORDINATE_H
#include<iostream>
using namespace std;
class Coordinate
{
friend Coordinate&operator-(Coordinate &c);
friend Coordinate operator+(Coordinate c1, Coordinate c2);//+的友元函数重载
friend ostream &operator<<(ostream&output, Coordinate&coor);//<<运算符的重载
public:
Coordinate(int x, int y);
Coordinate&operator ++();
Coordinate operator++(int);
//Coordinate &operator-();
//Coordinate operator+(Coordinate &c);+运算符成员函数的重载
int operator[](int index);//[]的重载
int getX();
int getY();
private:
int m_iX;
int m_iY;
};
#endif // !COORDINATE_H
Coordinate.cpp
#include"Coordinate.h"
Coordinate::Coordinate(int x, int y)
{
m_iX = x;
m_iY = y;
}
int Coordinate::getX()
{
return m_iX;
}
int Coordinate::getY()
{
return m_iY;
}
//Coordinate&Coordinate::operator-()
//{
// this->m_iX = -(this->m_iX);
// m_iY = -m_iY;
// return *this;
//
//}
Coordinate&Coordinate::operator++()
{
m_iX++;//++m_iX;
m_iY++;//++m_iY;
return *this;
}
Coordinate Coordinate:: operator++(int)
{
Coordinate old(*this);
this->m_iX++;
this->m_iY++;
return old;
}
Coordinate&operator-(Coordinate&c)
{
c.m_iX = -c.m_iX;
c.m_iY = -c.m_iY;
return c;
}
//Coordinate Coordinate::operator+(Coordinate&c)
//{
// Coordinate temp(0, 0);
// temp.m_iX = this->m_iX + c.m_iX;
// temp.m_iY = this->m_iY + c.m_iY;
// return temp;
//}加号运算符成员函数的重载
Coordinate operator+(Coordinate c1, Coordinate c2)
{
Coordinate temp(0, 0);
temp.m_iX = c1.m_iX + c2.m_iX;
temp.m_iY = c1.m_iY + c2.m_iY;
return temp;
}
ostream&operator<<(ostream&output, Coordinate &coor)
{
output << coor.m_iX << "," << coor.m_iY;
return output;
}
int Coordinate:: operator [](int index)//[]的重载
{
if (0 == index)
{
return m_iX;
}
if (1 == index)
{
return m_iY;
}
}
demo.cpp
#include"Coordinate.h"
#include<iostream>
using namespace std;
int main(void)
{
Coordinate coor1(1, 3);
Coordinate coor2(2, 4);
Coordinate coor3(0, 0);
coor3 = coor1 + coor2;
//cout << coor3.getX() << "," << coor3.getY() << endl;
cout << coor3 << endl;//<<的重载实现
cout << coor3[0] << endl;
cout << coor3[1] << endl;
system("pause");
return 0;
}
模板函数
demo.cpp
#include<iostream>
#include"stdlib.h"
using namespace std;
template<typename T>
void display(T a)
{
cout << a << endl;
}
template <typename T,class S>
void display(T t, S s)
{
cout << t << endl;
cout << s << endl;
}
template<typename T,int Ksize>//类型参数和变量
void display(T a)
{
for (int i = 0; i < Ksize; i++)
{
cout << a << endl;
}
}
int main(void)
{
display<double>(10.98);//模板函数
display<int, double>(5, 8.4);
display<int, 5>(6);
system("pause");
return 0;
}
类模板
MyArray.h
#pragma once
#ifndef MYARRAY_H
#define MYARRAY_H
#include<iostream>
using namespace std;
template<typename T,int Ksize,int KVal>
class MyArray
{
public:
MyArray();
~MyArray()
{
delete[]m_pArr;
m_pArr = NULL;
}
void display();
private:
T*m_pArr;
};
template<typename T,int Ksize,int KVal>
MyArray<T, KSize, KVal>::MyArray()
{
m_pArr = new T[Ksize];
for (int i = 0; i < Ksize; i++)
{
m_pArr[i] = KVal;
for (int i = 0; i < KSize; i++)
{
m_pArr[i] = KVal;
}
}
}
template <typename T,int KSize,int KVal>
void MyArray<T, KSize, KVal>::display()
{
for (int i = 0; i < KSize; i++)
{
cout << m_pArr[i] << endl;
}
}
#endif // !MYARRAY_H
MyArray.cpp
#include"MyArray.h"
demo.cpp
#include<stdlib.h>
#include<iostream>
#include<string>
#include"MyArray.h"
using namespace std;
int main(void)
{
MyArray<int, 5, 6>arr;
arr.display();
system("pause");
return 0;
}
demo.cpp
#include<iostream>
#include<stdlib.h>
#include<vector>
#include<list>
#include<map>
#include<string>
using namespace std;
int main(void)
{
vector<int>vec;
vec.push_back(3);
vec.push_back(4);
vec.push_back(6);
//vec.pop_back();
//cout << vec.size() << endl;
//
vector<int>::iterator itor = vec.begin();//使用迭代器操作遍历
//cout << *itor << endl;
//for (; itor != vec.end(); itor++)
//{
// cout << *itor << endl;
//}
//cout << vec.front() << endl;//打印第一个数据
//cout << vec.back() << endl;//打印最后一个数据
//list:
//list<int>list1;
//list1.push_back(4);
//list1.push_back(7);
//list1.push_back(10);
for (int i = 10; i < list1.size(); i++)
{
cout << list1[i] << endl;
}出现错误没法访问
//list<int>::iterator itor = list1.begin();
//for (; itor!=list1.end(); itor++)
//{
// cout << *itor << endl;
//}
map<int, string>m;
pair<int, string>p1(3, "hello");
pair<int, string>p2(6, "world");
pair<int, string>p3(8, "beijing");
m.insert(p1);
m.insert(p2);
m.insert(p3);
//cout << m[3] << endl;//索引
//cout << m[6] << endl;
map<int, string>::iterator citer = m.begin();
for (; citer != m.end(); citer++)
{
cout << citer->first << endl;
cout << citer->second << endl;
cout << endl;
}
system("pause");
return 0;
}