C++初学者

1、初学者最容易范的问题是把#include <iostream> 写成#include "iostream.h"

      在标准C++中所有的头文件都把.h后缀去掉了。

2、using namespace std。

     标准C++为了区分变量的命名把所有的函数等都封装在std下面,若在main之前不写using namespace std,则需要在函数的前面加上std::例如:std::cout<<"hi"。

使用命名空间例子:

#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
 string s_test_string;
 s_test_string="Welcome Use Std C++";
 cout<<s_test_string<<endl;
 return 0;
}

不使用命名空间的例子:

#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
 std::string s_test_string;
 s_test_string="Welcome Use Std C++";
 std::cout<<s_test_string<<std::endl;
 return 0;
}
注意:#include <iostream>不能写成#include <iostream.h>,其他标准C++中的包含文件同样。

3、一个简单的类模版代码:

#include <iostream>
#include <string>
using namespace std;
template <class type>
class TArray
{
public:
 explicit TArray(int sz = DefaultArraySize);
 TArray(type *Array,int Array_Size);
 TArray(const TArray &rhs);
 ~TArray(){delete []ia;};
 bool operator==(const TArray&) const;
 bool operator!=(const TArray&) const;
 bool operator=(const TArray&) const;
 int size() const{return _size;};
 void sort();
 int min() const;
 int max() const;
 void display();
 int find();
private:
 static const int DefaultArraySize;
 int _size;
 type *ia;
};
template<class type>
const int TArray<type>::DefaultArraySize=12;
int main(int argc, char* argv[])
{
 TArray<string> myTArray;
 myTArray.display();
 return 0;
}
template <class type>
TArray<type>::TArray(int sz)
{
 _size=sz;
 ia=new type[_size];
 for (int ix=0;ix<_size;ix++)
  ia[ix]=type();
}
template <class type>
TArray<type>::TArray(type *Array,int Array_Size)
{
 _size=Array_Size;
 ia=new type[_size];
 for (int ix=0;ix<_size;ix++)
  ia[ix]=TArray[ix];
}
template <class type>
TArray<type>::TArray(const TArray &rhs)
{
 _size=rhs._size;
 ia=new type[_size];
 for (int ix=0;ix<_size;ix++)
  ia[ix]=rhs.ia[ix];
}
template <class type>
void TArray<type>::display()
{
 for (int ix=0;ix<_size;ix++)
  cout<<"第"<<ix+1<<"个元素:"<<ia[ix]<<endl;
}
template <class type>
bool TArray<type>::operator=(const TArray& pTArray) const
{
 if (_size!=pTArray.size())
 {
  cout<<"数组大小不相等不能进行赋值"<<endl;
  return false;
 }
 for(int ix=0;ix<_size;ix++)
  ia[ix]=pTArray.ia[ix];
 return true;
}
template <class type>
bool TArray<type>::operator==(const TArray& pTArray) const
{
 if (_size!=pTArray.size())
  return false;
 for (int ix=0;ix<_size;ix++)
 {
  if (ia[ix]!=pTArray.ia[ix])
   return false;
 }
 return true;
}
template <class type>
bool TArray<type>:: operator!=(const TArray& pTArray) const
{
 if (_size==pTArray.size())
  return false;
 for (int ix=0;ix<_size;ix++)
 {
  if (ia[ix]==pTArray.ia[ix])
   return false;
 }
 return true;
}
注释:

1、bool operator=(const TArray&) const 重载赋值操作符号,等于号左边在函数中为this指针,也就是说我们可以直接操作类的局部变量。其他操作符的重载同样。

2、ia[ix]=type()类模版中使用type()为类型赋初始值。(空值)

3、注意模版类的定义语法。

4、const int TArray<type>::DefaultArraySize=12;注意局部静态常量的赋值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值