![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++开发之路
流岚明月
随随便便
展开
-
面向对象(下)09,templat类的成员函数template,用子类构造父类,类模板
#include<iostream>using namespace std;template<class f1, class f2>class pair{public: typedef f1 first_type; typedef f2 second_type; // f1 first 表示调用默认的无参构造函数 和 f1 first() 表示声明了一个返回T1的函数 f1 first; f2 second; //构造函数原创 2021-09-03 14:28:23 · 194 阅读 · 1 评论 -
指向父类的指针能指向子类,反之不行
子类和父类指针的问题,指向父类的指针,指向子类时候,能够使用属于父类的部分,而指向子类指针,不能再指向父类#include<iostream>using namespace std;class father{public: father(int a = 12) : fa(a) {}; int fa = 12;};class son : public father{public: son(int b = 2) : so(b) {}; int so ;};i原创 2021-09-03 13:55:32 · 170 阅读 · 0 评论 -
侯捷面向对象(下)04 类实现最基本的智能指针
#include<iostream>using namespace std;template<typename T> class shared_Ptr{private: T * px; long * pn;public: T& operator * () const { return *px; } T* operator ->() const { return px; } shared_Ptr( T* p)原创 2021-08-31 14:58:05 · 157 阅读 · 0 评论 -
侯捷面向对象设计(下)02转换函数- 默认构造将int类型转为Fraction
#include<iostream>using namespace std;/*转换函数练习将普通类型自动转换成类的类型2021.8.31*///将一个double转换为Fractionclass Fraction //表示分数的类{private: int son, mom; //分子分母public: Fraction(int s = 0, int m = 1) : son(s), mom(m) {} int get_son() const原创 2021-08-31 09:43:44 · 93 阅读 · 0 评论 -
侯捷面向对象设计(下)02转换函数-将类类型转为int
#include<iostream>using namespace std;/*转换函数练习2021.8.31*///将一个类转换为doubleclass Fraction //表示分数的类{private: int son, mom; //分子分母public: Fraction(int s = 0, int m = 1) : son(s), mom(m) {} //调用opertor函数,后面空一格,接类型加上(),无参数,转换不需要参数原创 2021-08-31 08:59:37 · 102 阅读 · 0 评论 -
侯捷-C++面向对象高级开发(上) 笔记09 string的简易实现
String.h部分#ifndef __MYSTRING__#define __MYSTRING__#pragma warning(disable:4996)#include<iostream>#include<string.h>#include<ostream>using namespace std;class String{private: char* m_date;public: String(const char* cstr =原创 2021-08-30 17:15:33 · 121 阅读 · 0 评论 -
侯捷-C++面向对象高级开发(上) 笔记06 complex的简易实现
complex.h头文件#pragma once#ifndef __COMPLEX__#define __COMPLEX__#include <iostream>#include<ostream>class complex{public: complex(double re = 0, double im = 0) : rel(re) , ima(im) //初始行和初始列 {} complex(const complex &am原创 2021-08-29 17:39:45 · 99 阅读 · 0 评论 -
侯捷-C++面向对象高级开发(上) 笔记02-04
侯捷C++面向对象高级开发1、防卫式声明用在头文件中#ifndef 头文件的大写名字#define 头文件的大写名字代码块#endif防卫式声明的作用是:防止由于同一个头文件被包含多次,而导致了重复定义。– “Car.h”,代码如下(并没有添加防卫式声明):// Car.hclass Car{// ...};– “Person.h”,代码如下(包含了Car.h文件):// Person.h#include "Car.h"class Person{public原创 2021-08-28 00:12:17 · 106 阅读 · 0 评论