资源描述
,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,C+Language Programming,Chapter 1:Introduction to Object,Chapter 2,:,making&using objects,Chapter 3,:,The c in C+,contents,the shortcoming of c,basic characteristics of Smalltalk,denifition,character of,oop,More about,iostreams,Reading and writing files,Controlling execution,basic characteristics of Smalltalk,(the pure,oop,),Everything is an object.,A program is a bunch of objects telling each other what to do by sending messages,.,Each object has its own memory made up of other objects,.,Every object has a type,.,All objects of a particular type can receive the same messages,.,1.3,denifition,:(class),class,describes a set of objects that have identical characteristics,(data elements),and behaviors(,functionality,),Object,is an,instance,of a class.,Interface:,The,requests you can make of an object are defined by its,interface,and the type is what determines the interface.,1.4character of,oop,a.The,hidden implementation,first reason for access control is to keep client programmers hands off portions they shouldnt touch,The second reason for access control is to allow the library designer to change the internal workings of the class without worrying about how it will affect the client programmer,1.4character of,oop,three access,specifiers,public,:means the following definitions are available to everyone.,private:,means that no one can access those definitions except you,the creator of the type,inside member functions of that type,protected:,acts just like private,with the exception that an inheriting class has access to protected members,but not private members.,1.4,character of,oop,b.Reusing,the implementation,The simplest way to reuse a class is to just use an object of that class directly,but you can also place an object of that class inside a new class.,c.Inheritance,Its nicer if we can take the existing class,clone it,and then make additions and modifications to the clone.This is effectively what you get with inheritance,1.4,character of,oop,polymorphism(,多态性,),:,-,多态性是指一般类中定义的属性和行为被特殊类继承之后,可以具有不同的数据类型或表现出不同的行为,这使得同一个属性和行为在一般类中和特殊类中具有不同的含义。,-,多态性是指同一消息,被不同类型的对型或相同类型的对象接受时产生不同的行为,.,1.5definition of interpreters and compilers.,An,interpreter,translates source code into activities(which may comprise groups of machine instructions)and immediately executes those activities.BASIC,for example,A,compiler,t,ranslates source code directly into assembly language or machine instructions.The eventual end product is a file or files containing machine code.,1.6 Declarations vs.definitions,A,declaration,introduces a name an identifier to the compiler.It tells the compiler“This function or this variable exists somewhere,and here is what it should look like.”,A,definition,on the other hand,says:“Make this variable here”or“Make this function here.”It allocates storage for the name.,1.7Your first C+program,Output,#include,/Stream declarations,using namespace std;,int,main(),cout,Hello,World!I am,8 Today!,endl,;/,1.7Your first C+program,使用标准名字空间,std,有,3,种方式,:,整体声明,using namespace std;,使用时单个对象临时声明,std:cout,Hello C+,对象,1,对象,2.,对象,n,;,cout,对象,1,对象,2.,对象,n,;,操纵符,作用,说明,oct,数据以,8,进制形式输出,作用范围为后续输出的整数对象,小数不起作 用。,dec,数据以,10,进制形式输出,(,默认,),hex,数据以,16,进制形式输出,endl,换行并刷新输出流,setw(n,),设置输出宽度,需包括头文件,iomanip,,仅对后一个对象起作用,setprecision(n,),设置输出小数位数,(,默认为,6),需包括头文件,iomanip,,作用范围为后续对象,(2)Program example,Character array concatenation,#include,using namespace std;,int,main(),cout,This is far too long to put on a single line but it can be broken up with no ill effects,nas,long as there is no punctuation separating adjacent character arrays.n;/:,(3)Introducing strings,#include,#include,using namespace std;,int,main(),string s1,s2;,string s3=Hello,World.;,string s4(I am);,s2=Today;,s1=s3+s4;,s1+=8;,cout,s1+s2+!,endl,;/:,(4)Reading and writing files,To open files for reading and writing,you must include,.To open a file for reading,you create an,ifstream,object,which then behaves like,cin,.To open a file for writing,you create an,ofstream,object,which then behaves like,cout,getline,(),which allows you to read one line(terminated by a newline)into a,string,object.,(4)example,#include,#include,using namespace std;,int,main(),string s;,ifstream,in(Scopy.cpp,);,ofstream,out(Scopy2.cpp);,while(getline(in,s),out s n;/:,(5)Introducing vector,#include,#include,#include,#include,using namespace std;,int,main(),vector v;,ifstream,in(Fillvector.cpp,);,string line;,while(getline(in,line),v.push_back(line,);,for(int,i=0;i,v.size,();i+),cout,i :,vi,endl,;/:,1.8Introduction to data types,For basic data type:,Char,:is,for character storage and uses a minimum of 8 bits,Int,:stores an integral number and uses a minimum of two bytes of storage.,Float;Double:,types store floating-point numbers,float,is for single-precision floating point,double,is for double-precision floating point,Data type(,contionue,),bool,type can have two states expressed by the built-in constants true.,true,&false,pointers,:,C and C+have a special type of variable that holds an address.This variable is called a pointer.,example:,int,a;,int,*,p=,Data,type(continue,),Reference:,is,an alias of a variable.,Example:,int,a;,int,Reference as argument,#include,using namespace std;,void,f(int,&r),cout,r=r,endl,;,cout,&r=&r,endl,;,r=5;,cout,r=r,endl,;,int,main(),int,x=47;,cout,x=x,endl,;,cout,&x=&x,endl,;,f(x,);/Looks like pass-by-value,/is actually pass by reference,cout,x=x,endl,;,/:,Data type(continue),A,struct,is a way to collect a group of variables into a structure.Once you create a,struct,then you can make many instances of this“new”type of variable youve invented.,/:C03:SimpleStruct.cpp,class Structure1,public:,char c;,int,i;,private:,float f;,double d;,;,Data,type(continue,),enum,enum,ShapeType,circle,square,rectangle,;,union,union Packed /Declaration similar to a class,char i;,short j;,int,k;,long l;,float f;,double d;,Data,type(contionue,),Arrays,are a kind of composite type because they allow you to clump a lot of variables together,one right after the other,under a single identifier name.,example:,int,a10,b33;,class:,1.10,Controlling execution,If-else(,分支结构,),The,if-else,statement can exist in two forms:with or without the,else,.,The two forms are:,(1),if(expression,)statement,or,(2),if(expression,)statement1,else statement 2,分支结构,Switch,(多分支结构),switch(selector,),case integral-value1:statement;break;case integral-value2:statement;break;case integral-value3:statement;break;case integral-value4:,statement;break,;case integral-value5:,statement;break,;,(.),default:statement;,分支结构,if(expression1),statement1,else if(expression2),statement2,else if(expression3),else,statementn,Controlling execution,while,while,do-while,and,for,control looping.A statement repeats until the controlling expression evaluates to,false,.,The form of a,while,loop is:,while(expression,)statement,#include,using namespace std;,int,main(),int,secret=15;,int,guess=0;,while(guess,!=secret),cout,guess;,cout,You guessed it!,endl,;/:,Controlling execution,do-while,The form of,do-while,is:,do,statement,while(,expression,);,for example:,#include,using namespace std;,int,main(),int,secret=15;,int,guess;,do,cout,guess;,while(guess,!=secret);,cout,You got it!,endl,;/:,Controlling execution,A,for,loop performs initialization before the first iteration.Then it performs conditional testing and,at the end of each iteration,some form of“stepping.”,The form of the,for,loop is:,for(initialization,;conditional;step),statement,For example,#include,using namespace std;,int,main(),for(int,i=0;i 128;i=i+1),if(i!=26),cout,value:i,character:,char(i,),function prototyping,(1)int,translate(float,x,float y,float z);or,int,translate(float,float,float);,defination,function,(,),(,1,)传值方式,#include,using namespace std;,void,change(int,a,int,b),int,t;,t=a;a=,b;b,=t;,void main(),int,x=3,y=4;,change(x,y,);,cout,”x=”x”,y=”y,endl,;,Function example,Pass address:,#include,using namespace std;,void,change(int,*,a,int,*b),int,t;,t=*a;*a=*b;*b=t;,void main(),int,x=3,y=4;,change(&x,&y,);,cout,”x=”x”,y=”y,endl,;,Function example,pass reference:(,引用方式,),#include,using namespace std;,void,change(int,&,a,int,&b),int,t;,t=a;a=,b;b,=t;,void main(),int,x=3,y=4;,change(x,y,);,cout,”x=”x”,y=”y,endl,;,
展开阅读全文