`
文章列表
带默认形参的构造函数与无参数的默认构造函数等价 在多个默认形参的构造函数中,第一个形式参数有默认值,编译器就认为是默认构造函数 #include <iostream> using namespace std; class A { public: A(){x=0;y=0;} //如果最左边有默认形参,则认为构造函数是默认构造函数,比如:A(int a=0,int b) 或 A(int a=0,int b=0) ; //反之不是默认构造函数,比如A(int a,int b=0) A(int a,int b=0, int c=0) A(int a,int ...
/** @file list3201.cpp */ /** Listing 32-1. Mystery Program */ #include <iostream> #include <ostream> struct point { point() : x_(0.0), y_(0.0) { std::cout << "default constructor\n"; } point(double x, double y) : x_(x), y_(y) { std: ...

static 例子

    博客分类:
  • c++
#include<iostream> class testStatic{ static int a,b; int c; public: testStatic( int _c):c(_c){ } static void output(testStatic & ts); int get(); }; int testStatic::a = 10; int testStatic::b = 20; void testStatic::output(testStatic & ts){ // std::cout<&l ...

const 关键字总结

    C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望对朋友们有所帮

指针的引用

#include<iostream> //void swap(const int *&p1, const int *& p2) // int 之前加const会导致swap函数错误,const不能修饰引用,引用不是常量 void swap( int *&p1, int *& p2) //此处函数的形参p1, p2都是引用 { // int *p; // *p=*p1; // *p1=*p2; // *p2=*p; } int main() { int *a,*b; std::cin>>*a>&g ...

模板的特化

#include <stdio.h> #include <string.h> //1. 这里我们先声明了一个通用类型的模板类。这里要有类型参数必须包含hashCode()方法。 //否则,该类型在编译期实例化时将会导致编译失败。 template <typename T> ...
#include<iostream> #include<vector> #include<list> template<typename T, int MAXSIZE> class MyContainer { public: MyContainer(){ std::cout<<MAXSIZE<<std::endl; } int capacity() const { return MAXSIZE; } private: T elements[MAXSIZE] ...
#include<iostream> #include<vector> #include<list> //1. 第二个类型参数的缺省值是vector<T> template<typename T, typename T2 = std::vector<T> > class MyClass { public: T2 data; MyClass() { std::cout<<" type 1. \n"; } void setData(){ ...
#include<iostream> //1. 标准模板类。 template<typename T1, typename T2> class MyClass { public: MyClass(){ std::cout<<" type 1. \n"; } }; //2. 两个模板参数具有相同类型的部分特化类。 template<typename T> class MyClass<T,T> { ...
C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static。前者应用于普通变量和函数,不涉及类;后者主要说明static在类中的作用。1.面向过程设计中的static1.1静态全局变量在全局变量前,加上关键字static,该变量就被定义成为一个静态全局变量。我们先举一个静态全局变量的例子,如下: //Example 1#include <iostream.h>void fn();static int n; //定义静态全局变量void main(){   n=20;   cout<<n<<endl;   fn();}v ...
Global site tag (gtag.js) - Google Analytics