C++捷径之五

2024-01-07 21:18
文章标签 c++ 之五 捷径

本文主要是介绍C++捷径之五,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

//  明成函数重运算符

three_d operator=(three_d op2);//类中定义,=运算符重载
three_d three_d::operator ++()//前置++;++a
{
 x++;
 y++;
 z++;
 return *this;
}

three_d operator++(int notused);

three_d three_d::operator ++(int notused)//后置++a++
{
 three_d temp=*this;
 x++;
 y++;
 z++;
 return temp;
}

friend three_d operator++(three_d &op1);//声明为友元,必须加上three_d &op1why?
 friend three_d operator++(three_d &op1, int notused);
//记住

friend three_d operator +(three_d op1,three_d op2);// 成员函数定义为友元
three_d operator +(three_d op1,three_d op2)//成员函数此时没有加作用域three_d::
{
 three_d  temp; 
 temp.x=op1.x+op2.x;
 temp.y=op1.y+op2.y;
 temp.z=op1.z+op2.z;
 return temp;
}

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

class sample{
 char *s;
public:
 static int elye;
 sample() { s=new char('/0'); cout<<"Normal constructor."; }
 sample(const sample &ob);
 ~sample() { if(s) delete []s;  cout<<"Freeing s./n";}
 void show() {cout<<s<<'/n';}
 void set( char *str);

 sample operator=(sample &ob);
};

int elye=0;
sample::sample(const sample &ob)
{
 cout<<"Copy constructor./n";
 s=new char[strlen(ob.s)+1];
 strcpy(s, ob.s);
}

void sample::set(char *str)
{
 s=new char[strlen(str)+1];
 strcpy(s, str);
}


sample sample::operator =(sample &ob)
{
 if(strlen(ob.s)>strlen(s))
 {
  delete []s;
  s=new char[strlen(ob.s)+1];
 }

 strcpy(s,ob.s);

 return *this;
}

sample input()
{
 char instr[80];
 sample str;

 cout<<"Enter a string: ";
 cin>>instr;

 str.set(instr);
 return str;
}

int main()
{
 sample ob; 
 ob=input();
 ob.show();

 return 0;
}

//程序没有调试通过,说

//

int operator[](int i) { return a[i];}//类中[]说明,数组

ob[2]//等价于ob.operator [](2)

three_d operator()(int a,int b,int c);//类中()说明

ob2=ob1(10,11,12);

str_type(char *str="") {strcpy(string,str);//类中字符串构造函数

str_type a("Hello ")//定义

//--程序#承的使用
class road_vehicle{
};

class truck: public road_vehicle{…};//truckroad_vehicle的派生类。私有不能直接调用

class derived:private base{…};//private派生,则基类中成员均为派生类的私有成员

base::set;//对于基类中定义的set()函数,使之派生类在私有派生情况下仍旧可用

class derived: protected base{…};// protected派生,则基类中成员均为派生类的私有成员

class derived: public base1,public base2{…};//多继承

//派生类的成员若于基类同名,则基于本身。即派生的对派生的,基的对基的

//基于保护的protected,被派生后相当于成为私有变量。

//明构造和析构

class base{

//public:

protected:

 base() { cout<<"Constructing base./n"; }

 ~base() { cout<<"Destructing base./n"; }

};

class derived: public base {

public:

 derived() { cout<<"Constructing derived./n"; }

 ~derived() { cout<<"Destructing derived./n"; }

};

int main()

{

 derived ob;

 return 0;

}

//结果:Constructing base.

//         Constructing derived.

//         Destructing derived.

//         Destructing base.

//先调用基类的构造函数,再调用派生类的,析构则相反。

//--程序#12  传递参数
 derived(int x, int y): base(y)
{…};//base(y)为基类中定义

ob.derived1::i=10;//基类中成员的使用

class derived1: virtual public base{…};//虚基类

 ob.i=10;//此时可以直接调用base中的成员,不需要ob.derived1::i=10

 

这篇关于C++捷径之五的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/581250

相关文章

C++中全局变量和局部变量的区别

《C++中全局变量和局部变量的区别》本文主要介绍了C++中全局变量和局部变量的区别,全局变量和局部变量在作用域和生命周期上有显著的区别,下面就来介绍一下,感兴趣的可以了解一下... 目录一、全局变量定义生命周期存储位置代码示例输出二、局部变量定义生命周期存储位置代码示例输出三、全局变量和局部变量的区别作用域

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决