c++ 友元函数,模板函数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liudao7994/article/details/82049136

友元函数.

类的友元函数是定义在类外部,但有权访问类的所有私有(private)成员和保护(protected)成员。尽管友元函数的原型有在类的定义中出现过,但是友元函数并不是成员函数。

友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类,在这种情况下,整个类及其所有成员都是友元。

如果要声明函数为一个类的友元,需要在类定义中该函数原型前使用关键字 friend

下面是友元函数的例子:

#include <iostream>

using namespace std;

class Box
{
   double width;
public:
   friend void printWidth( Box box );
   void setWidth( double wid );
};

// 成员函数定义
void Box::setWidth( double wid )
{
    width = wid;
}

// 请注意:printWidth() 不是任何类的成员函数
void printWidth( Box box )
{
   /* 因为 printWidth() 是 Box 的友元,它可以直接访问该类的任何成员 */
   cout << "Width of box : " << box.width <<endl;
}

// 程序的主函数
int main( )
{
   Box box;

   // 使用成员函数设置宽度
   box.setWidth(10.0);

   // 使用友元函数输出宽度
   printWidth( box );

   return 0;
}

打印结果:

Width of box : 10

详细看这里

http://www.runoob.com/cplusplus/cpp-friend-functions.html

模板函数

现在写个简单的代码 交换函数

#include <iostream>
#include <string>

using namespace std;

void myswap(int& a, int& b){
    int tmp = 0;
    tmp = a;
    a = b;
    b = tmp;

}

void myswap(char& a, char& b){
    char tmp = 0;
    tmp = a;
    a = b;
    b = tmp;
}

void main(){
    int a = 111;
    int b = 222;
    myswap(a, b);

    cout << "a = " << a << " b = " << b << endl;

    char c = '1';
    char d = '2';
    myswap(c, d);

    cout << "c = " << c << " d = " << c << endl;
    system("pause");
}

打印结果:

a = 222 b = 111
c = 2 d = 2
请按任意键继续. . .

如果现在又有两个double 类型的值需要交换,或者float 类型的值交换,怎么做. 继续写函数就不方便了.

在java中使用泛型来做类似的事情, 在c++中 使用模板函数.

使用模板函数

#include <iostream>
#include <string>

using namespace std;

//void myswap(int& a, int& b){
//  int tmp = 0;
//  tmp = a;
//  a = b;
//  b = tmp;
//
//}
//
//void myswap(char& a, char& b){
//  char tmp = 0;
//  tmp = a;
//  a = b;
//  b = tmp;
//}

template <typename T>
void myswap(T& a, T& b){
    T tmp = 0;
    tmp = a;
    a = b;
    b = tmp;
}

void main(){
    int a = 111;
    int b = 222;
    myswap(a, b);

    cout << "a = " << a << " b = " << b << endl;

    char c = '1';
    char d = '2';
    myswap(c, d);

    cout << "c = " << c << " d = " << c << endl;
    system("pause");
}

打印结果一样:

a = 222 b = 111
c = 2 d = 2
请按任意键继续. . .

和java里面的泛型 类似

模板类

实例定义了类 Stack<>,并实现了泛型方法来对元素进行入栈出栈操作

模拟入栈出栈操作

#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>

using namespace std;

template <class T>
class Stack { 
  private: 
    vector<T> elems;     // 元素 

  public: 
    void push(T const&);  // 入栈
    void pop();               // 出栈
    T top() const;            // 返回栈顶元素
    bool empty() const{       // 如果为空则返回真。
        return elems.empty(); 
    } 
}; 

template <class T>
void Stack<T>::push (T const& elem) 
{ 
    // 追加传入元素的副本
    elems.push_back(elem);    
} 

template <class T>
void Stack<T>::pop () 
{ 
    if (elems.empty()) { 
        throw out_of_range("Stack<>::pop(): empty stack"); 
    }
    // 删除最后一个元素
    elems.pop_back();         
} 

template <class T>
T Stack<T>::top () const 
{ 
    if (elems.empty()) { 
        throw out_of_range("Stack<>::top(): empty stack"); 
    }
    // 返回最后一个元素的副本 
    return elems.back();      
} 

int main() 
{ 
    try { 
        Stack<int>         intStack;  // int 类型的栈 
        Stack<string> stringStack;    // string 类型的栈 

        // 操作 int 类型的栈 
        intStack.push(7); 
        cout << intStack.top() <<endl; 

        // 操作 string 类型的栈 
        stringStack.push("hello"); 
        cout << stringStack.top() << std::endl; 
        stringStack.pop(); 
        stringStack.pop(); 
    } 
    catch (exception const& ex) { 
        cerr << "Exception: " << ex.what() <<endl; 
        return -1;
    } 
}

代码比较简单 ,很好的解释了模板类 既可以传入int 又可以传入stirng

结果:

7
hello
Exception: Stack<>::pop(): empty stack

详细看这里
http://www.runoob.com/cplusplus/cpp-templates.html

C++ list 也源码里也用了模板类 可以 list 转到定义看一下

class 转换 类型转换.

  • static_cast 普通值类型转换
  • const_cast 去常量
  • dynamic_cast 基类和派生类之间的转换

- reinterpret_cast 不通用 不常用 函数指针的转换 (一般在Void * 之间转)

  • const_cast (expr): const_cast 运算符用于修改类型的 const / volatile 属性。除了 const 或 volatile 属性之外,目标类型必须与源类型相同。这种类型的转换主要是用来操作所传对象的 const 属性,可以加上 const 属性,也可以去掉 const 属性。

  • dynamic_cast (expr): dynamic_cast 在运行时执行转换,验证转换的有效性。如果转换未执行,则转换失败,表达式 expr 被判定为 null。dynamic_cast 执行动态转换时,type 必须是类的指针、类的引用或者 void*,如果 type 是类指针类型,那么 expr 也必须是一个指针,如果 type 是一个引用,那个 expr 也必须是一个引用。

  • reinterpret_cast (expr): reinterpret_cast 运算符把某种指针改为其他类型的指针。它可以把一个指针转换为一个整数,也可以把一个整数转换为一个指针。

  • static_cast (expr): static_cast 运算符执行非动态转换,没有运行时类检查来保证转换的安全性。例如,它可以用来把一个基类指针转换为派生类指针。

详细看这里

http://www.runoob.com/cplusplus/cpp-casting-operators.html

异常捕获

和java 差不多

详细看这里
http://www.runoob.com/cplusplus/cpp-exceptions-handling.html

猜你喜欢

转载自blog.csdn.net/liudao7994/article/details/82049136