zhiyinjixu

  博客园  :: 首页  ::  :: 联系 ::  :: 管理

2011年11月14日

摘要: 阅读全文
posted @ 2011-11-14 17:15 zhiyinjixu 阅读(161) 评论(0) 推荐(0) 编辑

摘要: #include <fstream>#include <iostream>using namespace std;int main(){int a[10]={0}; ofstream outfile; outfile.open("d:\\aa\\f2.txt" , ios::out); if(!outfile) {cerr<<"open error!"<<endl; exit(1); } cout<<"enter 10 integer numbers:"<<end 阅读全文
posted @ 2011-11-14 16:36 zhiyinjixu 阅读(193) 评论(0) 推荐(0) 编辑

摘要: #include <iostream>using namespace std;class N //抽象基类{public: N() : m(0) {} virtual ~N() {} public: virtual void f() const = 0; //纯虚函数protected: int m; };class A : public N {public: A() :x(0) {} virtual ~A() {} public: virtual void f() const ; //定义纯虚函数protected: int x;};void A::f() c... 阅读全文
posted @ 2011-11-14 16:35 zhiyinjixu 阅读(198) 评论(0) 推荐(0) 编辑

摘要: 其实,不论是参数还是返回值,道理都是一样的,参数传入时候和函数返回的时候,初始化const变量const 和 参数1、函数形参表里没有const#include <iostream>using namespace std;void f (int a){++a;cout<<a<<endl;}int main(){int a = 5;f( 3 ); //括号里是字面常量还是变量都可以2、符号常量——值传递#include<iostream>using namespace std;void f (const int a){//++a; ... 阅读全文
posted @ 2011-11-14 16:33 zhiyinjixu 阅读(238) 评论(0) 推荐(0) 编辑

摘要: const成员函数①、const成员函数中不能对数据成员进行修改;②、const成员函数中不能调用非const成员函数;③、const成员函数中可以调用非const的非成员函数。注意:①一般将不修改数据成员的成员函数都定义为const成员函数,只是一种习惯;②const函数只能是成员函数,一般函数是不能给加const,如:f() const {}int main () {}这就是错误的,因为const函数是对类中的成员函数而言的。void f4() { }class A{public: int x; void f1() {} void f2() const ;//const成员函数 vo... 阅读全文
posted @ 2011-11-14 16:22 zhiyinjixu 阅读(137) 评论(0) 推荐(0) 编辑

摘要: 有关const的用法总结:一、const和指针判断规律:如果const位于星号的左侧,则const就是用来修饰指针所指向的变量,即指针所指的变量不能修改;如果const位于星号的右侧,const就是修饰指针本身,即const指针。int a = 500;const int* p = &a ; //第一大类: 指向const的指针int const *p = &a ; //和上面的没有区别int* const p = &a ;//第二大类: const指针const int* const p = &a ; //第三大类: 指向const的const指针第一大类:指 阅读全文
posted @ 2011-11-14 16:19 zhiyinjixu 阅读(209) 评论(0) 推荐(0) 编辑

摘要: 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。我们可以用=进行赋值操作,==进行比较,+做串联(是不是很简单?)。我们尽可以把它看成是C++的基本数据类型。首先,为了在我们的程序中使用string类型,我们必须包含头文件<string>。如下:#include<string>//注意这里不是string.hstring.h是C字符串头文件1.声明一个C++字符串声明一个字符串变量很简单:stringS 阅读全文
posted @ 2011-11-14 16:17 zhiyinjixu 阅读(182) 评论(0) 推荐(0) 编辑

摘要: 常量:(不可以定义结构型常量)常量包括字面常量,符号常量,契约型常量,枚举常量这几种类型。const int a=3;这里的 a 就是符号常量,3 就是字面常量。一、字面常量(也叫直接常量)包括:整形常量,字符型常量,字符串常量。注意:不存在数组常量,结构体常量等结构型的字面常量。但是存在结构型的符号常量二、符号常量:(可以定义结构型常量)用#define和const定义的常量!这两种常量之间的区别:1、#define定义的常量,除了字符串字面常量外都不占内存,所以无法取常量的地址,仅仅是宏替换而已eg:#define NAME“pang dong”本质是字符串字面常量,会占用“静态存储区”# 阅读全文
posted @ 2011-11-14 16:15 zhiyinjixu 阅读(238) 评论(0) 推荐(0) 编辑

摘要: 一、有子对象的派生类的构造函数:#include <iostream>using namespace std;class A{public: A(int tam,int tai):am(tam),ai(tai) {}public: int am;protected: int ai;};class B:public A{public: B(int tam,int tai, int tob_Am,int tob_Ai, int tbm,int tbx) : A(tam,tai), ob_A(tob_Am,tob_Ai) { bm=tbm; bx=... 阅读全文
posted @ 2011-11-14 16:14 zhiyinjixu 阅读(240) 评论(0) 推荐(0) 编辑

摘要: 运算符重载的两种分类:一、作为类成员#include <iostream>using namespace std;class complex{public: complex (){} complex (int x, int y) {real = x; imag = y;}public: void display ();private: int real; int imag;public : complex operator+ (complex& ob2);};complex complex::operator+ (complex& ob2) //以c2为实参调用c1 阅读全文
posted @ 2011-11-14 16:06 zhiyinjixu 阅读(224) 评论(0) 推荐(0) 编辑

摘要: #include <iostream.h>class A{public:A(){}A(int a,int b):m(a),n(b) {}public:int m;int n;friend ostream& operator<< (ostream&, A&);friend istream& operator>> (istream&, A&);};ostream& operator<< (ostream& out, A& e){out<<e.m<<&quo 阅读全文
posted @ 2011-11-14 16:03 zhiyinjixu 阅读(159) 评论(0) 推荐(0) 编辑

摘要: 不同数据类型间的转换的分类:一、转换构造函数——定义在目标类型的类中。 1、类之间的转换构造函数2、内置类型的转换构造函数二、类型转换函数——定义在源类型的类中。举例代码:一、转换构造函数 1、类之间的转换构造函数//将类B的对象转换为类A的对象#include <iostream>using namespace std;class B{public:int x;double y; };class A{public:A(){}A(B& t){ m=t.x; n=t.y; } //类之间的转换构造函数 ,和复制构造函数有些类似int m; double n;};int mai 阅读全文
posted @ 2011-11-14 15:55 zhiyinjixu 阅读(167) 评论(0) 推荐(0) 编辑

摘要: 规律:比如i=3;无论是++i 还是 i++,运算后,i的值都是4;而对于(++i)的值是4,(i++)的值还是3;例如:#include <iostream>using namespace std;int main (){int i,k;i= 3;k= 0; k=++i; cout<<k<<"\t"<<i<<endl; i= 3;k= 0; k=i++; cout<<k<<"\t"<<i<<endl;cout<<endl;i= 3;k 阅读全文
posted @ 2011-11-14 15:48 zhiyinjixu 阅读(184) 评论(0) 推荐(0) 编辑

摘要: #include <iostream>using namespace std;class A{public: A() { } A(A& e) { x = e.x ; } int x;};int main (){ A ob1; ob1.x = 4; A ob2(ob1); cout<<ob2.x<<endl;} 阅读全文
posted @ 2011-11-14 15:42 zhiyinjixu 阅读(174) 评论(0) 推荐(0) 编辑

摘要: #include <iostream>using namespace std;class A{public:static int a; //定义静态成员变量static void f(); //定义静态成员函数};int A::a = 2; //静态成员变量不同于普通的成员变量,还必须在类外面定义一次 ,此时可以初始化void A::f() ... 阅读全文
posted @ 2011-11-14 15:40 zhiyinjixu 阅读(182) 评论(0) 推荐(0) 编辑

摘要: 一、友元函数:①普通函数作为类的友元:#include <iostream>using namespace std;class A{private:int i;friend void f() ;}; void f(){A jack;jack.i=20;cout<<jack.i<<endl;}int main (){f();return 0;} ②一个类的成员函数作为这个类的友元:#include <iostream>using namespace std;class A{private:int i;public:void f();friend v 阅读全文
posted @ 2011-11-14 15:34 zhiyinjixu 阅读(201) 评论(0) 推荐(0) 编辑

摘要: 一般函数模板的书写格式:template < 函数模板形参列表>返回类型 函数名 (函数形参列表){函数体}函数模板的特化的完整书写格式:template < >返回类型 函数名 <模板实参列表> (函数形参列表) //比起一般函数模板的书写格式来,多了 <模板实参列表>{函数体}当模板的返回类型和函数形参的类型相同的时候,模板实参列表可以省略:template < >返回类型 函数名 (函数形参列表) //比起函数模板的特化的完整书写格式来少了 <模板实参列表>,和一般函数书写模式大致相同了{函数体}函数模板的特化的完整 阅读全文
posted @ 2011-11-14 15:29 zhiyinjixu 阅读(181) 评论(0) 推荐(0) 编辑

摘要: 规律:调用函数模板时的里放的依次是模板参数列表中定义的T1, T2,T3····的对应数据类型。调用函数模板时的里和模板参数列表中的是一一对应的,无论是个数上,还是数据类型上。②处是由①决定的,不牵扯到其他地方的事情。即:在下面的例子中(1):②处的放的是三个数据类型,是因为有,,三个,也就是说这两处的个数是相等的;(2):这两处的数据类型是一一对应的相关代码:#pragma once#include <iostream>using namespace std;template <typename T1,typename T2,type 阅读全文
posted @ 2011-11-14 15:19 zhiyinjixu 阅读(139) 评论(0) 推荐(0) 编辑

摘要: 数组的引用#include <iostream>using namespace std;void f(int (&rs)[6]) //这里的数组长度必须是确定的{ cout<<rs[0]<<endl;}int main(){ int s[]={5,6,7,8,9,6}; f(s); cout<<endl;}函数模板中传递数组的引用#include <iostream>using namespace std;template <typename T,int size>void f(T (& s)[size]) 阅读全文
posted @ 2011-11-14 15:15 zhiyinjixu 阅读(340) 评论(0) 推荐(0) 编辑

摘要: 非类型模板参数-- Nontype template parameters模板的参数不一定是要是类型,也可以是值,以下面的模板为例:#include<iostream>usingnamespace std;template <typename T>T add(const T &a, const T &b){ return a+b;}void main(){ cout << "5 add 10 is:" << add<>(5,10) << endl;}参数T表明了一个未知的类型,或者说T代 阅读全文
posted @ 2011-11-14 15:10 zhiyinjixu 阅读(360) 评论(0) 推荐(0) 编辑