C++篇-----开篇(3)

已经撸完两篇了,继续撸~~~~~~~~~~~~~~~~~~~~

1.运算符重载:

  在类中定义一个特殊的函数,以便通知编译器,遇到该重载运算符时调用该函数,并由该函数来完成该运算符应该完成的操作

<函数类型><类名>::operator<重载的运算符>(<形参表>)

{

}

注意:重载的运算符必须是一个已有的合法的C++运算符。

           当重载一个运算符时,该运算符的操作数个数,优先级和结合性不能改变

例子:

/**************************************Count.h******************************************************/

#pragma once
class CCount
{
public:
CCount(int s);
~CCount(void);
private:
int sum;
public:
int operator++();
int operator++(int);
};


/**************************************************Count.cpp************************************/

#include "Count.h"
CCount::CCount(int s)

sum = s;
}
CCount::~CCount()
{
}
int CCount::operator++()//自增运算符是前缀运算符
{
return (++sum);
}

int CCount::operator++(int)//自增运算符是后缀运算符
{
return (sum++);
}


/*********************************************主函数******************************************/

#include "Count.h"
CCount::CCount(int s)

sum = s;
}
CCount::~CCount()
{
}
int CCount::operator++()//自增运算符是前缀运算符
{
return (++sum);
}


int CCount::operator++(int)//自增运算符是后缀运算符
{
return (sum++);
}

2.重载二元运算符

/**************************Math.h**************************************/

class CMath

{

public:

     CMath(int score);

    ~CMath(void);

private:

    int score;

public:

    int   operator+(CMath myScore);

     int  operator+(int a);

}

/***********************Math.cpp************************************/

#include"Math.h"

CMath::CMath(int score)

{

}

CMath::~CMath()

{

}

int CMath::operator+(CMath myScore)           //重载运算符

{

   return(this->score+myScore.score);

}

int CMath::operator+(int a)                     //重载运算符

{

  return( this->score+ a);

}


/*******************operatorScore.cpp**********************************/

#include" Math.h"

#include <iostream>

using namespace std;

int main()

{

  CMath m1(70);

  CMath m2(80);

 

   cout<<m1+m2<<endl;   //原本两个对象是不能直接相加的,需要重载运算符

   cout<<m1+50<<endl;  //原本对象和数字是不能直接相加的,需要重载运算符

  system("pause");

  return 0;

}


3.函数模板

具体函数的抽象形式

 形式: template<class 类型名>{class 类型名2}.....>

函数模板的定义

template<class Type1,class Type2>

Type1 sum(Type1 m1,Type2 m2)

{

  return (m1+m2);

}

隐式实例化例子:

#include <iostream>
using namespace std;

template <class T1,class T2>
T1 sum(T1 x, T2 y)
{
return (x+y);
}
int main()
{
cout << sum(2, 'a') << endl;
cout << sum('a', 2) << endl;
cout << sum(2.2, 2.5) << endl;
cout << sum(2.2, 5) << endl;   
/****分别输出99,c,4.7,7.2**********************/

system("pause");
return 0;
}


显示实例化例子:

#include <iostream>
using namespace std;
template <class T1,class T2,int baseNum>
T1 sum(T1 x, T2 y)
{
return  (x+y)*baseNum;
}
int main()
{
cout << sum<int,char,2>(2, 'a') << endl;
cout << sum<char, int, 2>('a', 2) << endl;
cout << sum<float,float, 2>(2.2, 2.5)<< endl;
cout << sum<float,int, 2>(2.2, 5) << endl;

system("pause");
return 0;
}


4.类模板

  /***************Sum.h**************************/
#pragma once
#include<iostream>
using namespace std;
template<typename T1, typename T2>
class CSum
{
public:
CSum()
{
result = 0;
}
~CSum()
{
}
private:
T1 result;
public:
void sum(T1 x, T1 y)
{
result = x + y;
}
void sum(T1 x, T2 y)
{
result = x + y;
}
void show(void)
{
cout <<"result= "<< result << endl;
}
};


/*****************************main.cpp*************************/
#include <iostream>
#include "Sum.h"
using namespace std;

int main()
{
CSum<char,double> one;  //char 替代T1,double 替代 T2
one.sum(2, 'a');
one.show();
system("pause");
return 0;
}

6.输入输出流




cout.setf(ios::showpos);
cou.width(10);
cout.fill(' # ');填充#字符
cout.setf(ios::scientific);//科学计数法
cout.precision(8);//设置精度

使用格式算子setw(n)
记得添加头文件#include <iomanip>

7 文件的读写和关闭

 读写文件
方式一:使用提取运算符或者插入运算符对文件进行读写操作
char ch;
infile>>ch;
outfile<<ch;
方式二:使用成员函数get,getline,read,put,write进行文件的读写操作

例子:
#include<iostream.h>
#include<fstream>
using namespace std;
int main()
{
  fstream file1;
  file1.open("myfile.txt",ios::in);//in说明对这个文件进行读操作
  if(!file1)
{
  cout<<"文件打开失败"<<endl;
}
  fstream file2;
  file2.open("myfile2.txt",ios::out || ios:: trunc);//out说明对这个文件写操作,trunc是清除原来的文件内容
  if(!file2)
{
  cout<<"文件file2打开失败"<<endl;
}

char ch;
while(!file1.eof())
{
   file1.read(&ch,1);
   cout<<ch;
   file2.write(&ch,1);
}
file2.close();
file1.close();
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值