随笔分类 -  C++基础学习

运算符 优先级 结合性
摘要:C语言的运算符可分为以下几类:1.算术运算符:用于各类数值运算。包括加(+)、减(-)、乘(*)、除(/)、求余(或称模运算,%)、自增(++)、自减(--)共七种。2.关系运算符:用于比较运算。包括大于(>)、小于(<)、等于(==)、大于等于(>=)、小于等于(<=)和不等于(!=)六种。3.逻辑运算符:用于逻辑运算。包括与(&&)、或(||)、非(!)三种。4.位操作运算符:参与运算的量,按二进制位进行运算。包括位与(&)、位或(|)、位非(~)、位异或(^)、左移(<<)、右移(>>)六种。5.赋值运算符:分为简单 阅读全文
posted @ 2011-12-03 19:50 Gavin Dai 阅读(4332) 评论(0) 推荐(0)
C++初始化与赋值
摘要:先来个区别说明:赋值操作是在两个已经存在的对象间进行的,而初始化是要创建一个新的对象,并且其初值来源于另一个已存在的对象。编译器会区别这两种情 况,赋值的时候调用重载的赋值运算符,初始化的时候调用拷贝构造函数。如果类中没有拷贝构造函数,则编译器会提供一个默认的。这个默认的拷贝构造函数只是 简单地复制类中的每个成员。下面看例子。 c++中初始化和赋值操作差别是很大的。 对于基本数据类型差别不大: 比如: inta=12;//initialization,copy0X000Ctoa a=12;//assignment,copy0X000Ctoa 但是对用户自定义的数据类型比如String初始化和赋 阅读全文
posted @ 2011-12-02 10:56 Gavin Dai 阅读(511) 评论(0) 推荐(0)
C++的cin/cout高阶格式化操作
摘要:这篇文章主要讲解如何在C++中使用cin/cout进行高级的格式化输出操作,包括数字的各种计数法(精度)输出,左或右对齐,大小写等等。通过本文,您可以完全脱离scanf/printf,仅使用cin/cout来完成一切需要的格式化输入输出功能(从非性能的角度而言)。更进一步而言,您还可以在<sstream>、<fstream>上使用这些格式化操作,从而代替sscanf/sprintf和fscanf/fprintf函数。为方便描述,下文仅以cin/cout为例进行介绍。一、综述cin/cout是STL库提供的一个iostream实例,拥有ios_base基类的全部函数和成员 阅读全文
posted @ 2011-11-27 15:19 Gavin Dai 阅读(663) 评论(0) 推荐(0)
输放输出格式控制
摘要:#include <iostream>using namespace std;int main(){int a=21;cout.setf(ios::showbase);cout<<"dec:"<<a<<endl;cout.unsetf(ios::dec);cout.setf(ios::hex);cout<<"hex:"<<a<<endl;cout.unsetf(ios::hex);cout.setf(ios::oct);cout<<"oct:&quo 阅读全文
posted @ 2011-11-15 18:42 Gavin Dai 阅读(231) 评论(0) 推荐(0)
自加运算符的前置与后置的重载
摘要:#include <iostream>using namespace std;class Time{public: Time(){minute=0;sec=0;} Time(int m,int s):minute(m),sec(s){} Time operator++(); Time operator++(int); void display(){cout<<minute<<":"<<sec<<endl;} private: int minute; int sec;};Time Time::operator++() 阅读全文
posted @ 2011-11-15 16:57 Gavin Dai 阅读(362) 评论(0) 推荐(0)
运算符重载
摘要:#include <iostream>#include <string>using namespace std;class String{public: String(){p=NULL;} String(char *str); friend bool operator>(String &string1,String &string2); friend bool operator<(String &string1,String &string2); friend bool operator==(String &strin 阅读全文
posted @ 2011-11-15 16:32 Gavin Dai 阅读(271) 评论(0) 推荐(0)
子类对象赋值给父类引用
摘要:#include <iostream>using namespace std;class Point{public:Point(float=0,float=0);void setPoint(float,float);float getX() const {return x;}float getY() const {return y;}friend ostream & operator<<(ostream &,const Point &);protected:float x,y;};Point::Point(float a,float b){x=a 阅读全文
posted @ 2011-11-14 19:46 Gavin Dai 阅读(2097) 评论(0) 推荐(0)
字符串比较
摘要:#include <iostream>#include <string>using namespace std;int main(){ void max_string(char str[][30],int i);int i;char country_name[3][30];for(i=0;i<3;i++)cin>>country_name[i];max_string(country_name,3);return 0;}void max_string(char str[][30],int n){int i;char string[30];strcpy(s 阅读全文
posted @ 2011-11-13 22:37 Gavin Dai 阅读(188) 评论(0) 推荐(0)
二分法解方程
摘要:#include <cmath>#include <iostream>using namespace std;float f(float x){return x * x * x - 5 * x *x + 16 * x - 80;}void main(void){float x1, x2, x0, f0, f1, f2;do {cout<<"Input x1, x2\n";cin>>x1>>x2;f1 = f(x1);f2 = f(x2);} while (f1 * f2 > 0);do {x0 = ( x1 阅读全文
posted @ 2011-11-13 22:01 Gavin Dai 阅读(264) 评论(0) 推荐(0)
一个类的成员函数做为另一个类的友元函数
摘要:#include <iostream>using namespace std;class Date;class Time{public:Time(int,int,int);void display(const Date&);private:int hour;int minute;int sec;};class Date{public:Date(int,int,int);friend void Time::display(const Date &);private:int month;int day;int year;};Time::Time(int h,int m, 阅读全文
posted @ 2011-11-13 21:36 Gavin Dai 阅读(3418) 评论(0) 推荐(0)
指向类成员函数的指针
摘要:#include <iostream>using namespace std;class Time{public:Time(int,int,int);int hour;int minute;int sec;void get_time();};Time::Time(int h,int m,int s){hour=h;minute=m;sec=s;}void Time::get_time(){cout<<hour<<":"<<minute<<":"<<sec<<endl;}i 阅读全文
posted @ 2011-11-13 20:27 Gavin Dai 阅读(275) 评论(0) 推荐(0)
函数的默认参数与内联函数
摘要:函数分声明和定义,但是如果声明和定义是在一些起的,相当于没有声明的话,默认参数与inline就只要写在定义中就可以了。但是如果函数如果先声明后定义的话,默认参数只能写在声明中,定义时不能再写上默认参数。inline在声明中写,定义时可写可不写. 阅读全文
posted @ 2011-11-13 15:10 Gavin Dai 阅读(293) 评论(0) 推荐(0)
变量声明和定义的区别
摘要:我们在程序设计中,时时刻刻都用到变量的定义和变量的声明,可有些时候我们对这个概念不是很清楚,知道它是怎么用,但却不知是怎么一会事,下面我就简单的把他们的区别介绍如下:(望我的指点对你受益)变量的声明有两种情况:1、一种是需要建立存储空间的。例如:int a 在声明的时候就已经建立了存储空间。2、另一种是不需要建立存储空间的。 例如:extern int a 其中变量a是在别的文件中定义的。前者是“定义性声明(defining declaration)”或者称为“定义(definition)”,而后者是“引用性声明(referncing declaration)”,从广义的角度来讲声明中包含着定 阅读全文
posted @ 2011-10-24 15:41 Gavin Dai 阅读(70957) 评论(12) 推荐(28)