You are on page 1of 50

1 C++

.
1.1
1.1 .1

Structured Programming---SP

---

- --+=+


1

2
1

Object Oriented


Object----


Object


AttributeMethod








:
:
()







Class
Class






OOP








---




Message
Message












stud.display();
stud display display
3

stud display()
stud.display();

. . Object-Oriented
Object-Oriented Programming ----OOP
----OOP

---

--



= =
++
=+

OOP


---+

OOP
1
2
3

OOP
1


----


---

---


----
------ char , char , int int
------ GetCloth()EatFood()GoHome()Step()

------ AddPerson()DeletePerson()PrintData()Change()



------ int Hour, int Minute, int Second
------ SetTime()ShowTime()
2 Encapsulation




--- ,

public ---
private ----

class Watch
{ public:
void SetTime(int NewH, int NewM, int NewS);
void ShowTime();
private: int Hour, minute, Second;
};


C++ C++

3 Inheritance
Inheritance

C++

- ---



---
---






4 Polymorphism
Polymorphism

--
----



- ---
-
----


1






2




3

. . C++
C++

C C++
C
1C 32
2
3
4
5

1
2C

3
C C++
C++
C++
C++ C C C++

1980 Bjarne StroutStrup C


BCPLSimula67 Alogl68
C++
1983 C++
3 C++1994 ANSI C++
C++
C++ C

C++
C C++
C
C+++C
+

C
C++ C C


OOP
C++
OOP





#include "stdio.h"
void main(void)
{ int x,y,sum; //
scanf("%d%d",&x,&y);
sum=add(x,y);
printf("%d",sum);
}
int add(int a, int b) //
{ return a+b;
}
--- + =+


1


---








----


----
2


#include "iostream.h"
class AddClass //
{ public:
void Add(int x, int y) //


{ sum=x+y; }
/*void Add(float x, float y)
{ sumf=x+y; }*/
void printSum(int flag)
{ cout<<"Total Sum="<<sum; }
/*void printSum(float flag)
{ cout<<"Total Sum="<<sumf; }*/
private: int sum; //

//float sumf;
};
void main(void)
{ AddClass addObj; //
1+2 =
addObj.Add(1,2);//

//addObj.Add(3.4F, 5.6F);
addObj.printSum(1); //
//addObj.printSum(1.0F);
}

---+








1.2 C++
C++
C++ C
C++
C++

int
float
double

char
w-char
bool
void

struct

union
enum

class

1-1 C++


C++ C
C++

void
void

void
void a; //

. .

C++
C++

#include iostream.h

void main(void)
{ int a,b;
cout << This Is C++ Program ! //
cout <<endl // <<
cin>>a>>b; //
cout <<a+b=<<a+b<<endl
}

stdio.h iostream.h cout


cin C++
I/O
printf() cout<<

cout<< 1<< 2<<<< n
scanf() cin>>

cin>> 1>> 2>>>> n
C++
C++ endl C \n
C *.C *.CPP(C Plus Plus)

1#include <iostream.h> //

void main()
{ double length,width; // ---
cout<<"Enter length: "; //
cin>>length; //
cout<<"Enter width: "; //
cin>>width; //
cout<<"length is: "<<length<<",width is: "<<width<<,area
is:<<length* width <<endl;
}
C++ / C /
C++ C /
cin>>

C++
C /* */
// //
C++
#include <iostream.h>
void main()
{ cout<<This is my first C++ program.\n;
// This is my first C++ program.
/*
This is my first C++ program.*/
}
C++
C++
C++
asm auto break case catch
char class const continue default
delete do double else enum
extern float for friend goto
if inline int long new
operator private protected public register
return short signed static struct
switch template this throw try
typedef union unsigned virtual void
volatile while
47 C
C++
C++
catch
class
delete
friend
inline
new
operator
private
protected
public
template
this
throw
try
virtual
4 C++
C++
C++ C

C++

C++
Borland C++ turbo C++ 32
C++


5 const
1
const =
const =
const int sumValue=10 //
const int *ptr //
void Function(const int & X) //
{
}

1#include<iostream.h>
#include<math.h>
const double pi=3.1415926;
void main()
{ int m;
cout<<please enter the number<<endl;
cin>>m;
double I=m*pi/180;
double x=sin(i);
double y=cos(i);
double z=tan(i);
cout<<sin(<<m<<)=<<x<<endl;
cout<<cos(<<m<<)=<<y<<endl;
cout<<tan(<<m<<)=<<z<<endl;
}
3 C++
C++ const
const

C #define
#define
#define sumValue 10 // sumValue char int
4 const
const
const int sumValue=10
sumValue=0 //
void Display(const int *ptr, int n)
{ cout<< ptr[0] //==*(ptr+0),
ptr[0]=1 //( )
*ptr=1; //()
}
5 const :




double const
double *dp; dp

const int *ptr=&X;


int const *ptr=&X;
ptr const int



const * *
*ptr=1
ptr ptr=&Y

const char *s=Hello!;


char const *s=Hello!;
s=Hi!;
*s=Y;
void fun(const int * ptr)
{ *ptr=1; //
}

1
#include <iostream.h>
void main()
{ const double * dp;
double dVal1 = 123.456;
double dVal2 =234.234;
dp = &dVal1;
//*dp = 789.012; //
dp =&dVal2; //
cout << *dp << endl;
}

2
#include <iostream.h>
const char *getmonth(int mon) //
{ static char *month[]={
,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oc
t,Nov,Dec};
return month[mon];
}
void main()
{ int m;
cout<<Please input the number of month:;
cin>>m;
cout<<getmonth(m);
}

getmonth() const char *


getmonth(7)[2]=n 7 Jun

int *const ptr=&X


const ptr
ptr=&Y ptr int ptr
*ptr=2; X=2 ptr
int ptr[10];
char * const s=Hello!;
s=Hi!;
*s=Y;

1
#include <iostream.h>
void main()
{
double dVal1 = 123.456;
double * const dp = &dVal1; //
double dVal2 =234.234;
*dp = 789.012; //
//dp =&dVal2; //
cout << *dp << endl;
}

const int *const ptr=&X


int const * const ptr=&X
ptr

const int

1
#include <iostream.h>
void main()
{ int y=10, x=5;
int *const p=&x;
cout<<x<<, <<&x <<, <<p<<endl;
//p=&y;//error
*p=20;
cout<<x<<, <<*p<<endl;
const int *p1;
p1=&x;
cout<< *p1<<endl;
p1=&y;
cout<< *p1<<endl;
//*p1=6;error
}
6 const :
int sum=1; // const
sum sum=2; PC
16
const int sum=1; // const
sum sum=2; C++ const

const int ArraySize=10
char array[ArraySize]
7 C #define C++
C++ const
#define
const const
#define const ;
#define Min 1
const int Max=100;
void main(void)
{ #define Min 2 //
const int Max=200; //
}

const
1
#include "iostream.h"
double *fn(const double *pd)
{ static double ad=32;
ad+=*pd;
cout<<"fn being called....the value is:"<<*pd<<endl;
return &ad;
}
void main()
{ double a=345.6;
const double *pa=fn(&a);
cout<<*pa<<endl;
a=55.5;
pa=fn(&a);
cout<<*pa<<endl;
}
fn being called....the value is:345.6
377.6
fn being called....the value is:55.5
433.1

2 0

#include "iostream.h"
int instr(const char *substr, const char *str)
{ const char *sub,*s;
int count=0;
while(*str!=\0)
{ for (s=str,sub=substr;*s==*sub;s++,sub++)
if(!*(sub+1)) //
count++;
str++;
}
return count;
}
void main()
{ char s1[100],s2[100];
cout<< ;
cin>>s1;
cout<<;
cin>>s2;
cout<<<<instr(s2,s1)<<endl;
}

Iamateacher!
a
3

6
1
C++ C++
C++



C++




C++
void
C++

void return

1
#include <iostream.h>
double power (double x, int n); //
void main(void)
{
cout << "5 to the power 2 is " << power(5,2) << endl;
//
}
double power (double x, int n)
{ double val = 1.0;
while (n--)
val *= x;
return(val);
}
5 to the power 2 is 25
2

1 C

int add(int X, int Y)
{ return X+Y; }
void main()
{ add(1,2); //
add(1); //
add(1.1,2.2); //
}
2 C++
C++
C
C++

3
C++

4 =

int add(int X=5, int Y=6)
{ return X+Y;
}
void main(void)
{ add(10,20); //10+20
add(10); //10+6
add(); //5+6
}
5

int add(int X, int Y=5, int Z=6); //


int add(int X=1, int Y=5, int Z); // add(3,4)
int add(int X=1, int Y, int Z=6); // add()
C

int add(int X=5, int Y=6); int add(int X=5, int Y=6)
void main(void) { return X+Y; //
{ add(); // }
} void main(void)
int add(int X, int Y) { add();
{ return X+Y; } }
7

int add(int X=1, int Y=2)//


void main(void)
{ int add(int X=3, int Y=4)//
add()//
}
void fun(void)
{ add()//
}
8 ,

9 2

1 (

void f0(float x, int y, char z);


void f1(float x, int y, char z=`B`);
void f2(float x, int y=4, char z=`B`);
void f3(float x=1, int y=4, char z=`B`);

float a=2.1; int b= 5; char c=`C`;
f3(a, b, c); // a, b, c
f3(a,b); //a, b, `B`
f3( ); //1, 4, `B`
f1(a,b); //a, b, `B`
f0(a,b,c); //a, b, c
f0(a,b); f1(a); f2( ); //

2#include <iostream.h>
display(char *p="Visual C++ 6.0") //
{ cout<<p<<endl; }
//main() display()
void main()
{ char *sp="Visual Basic 6.0";
display(); //
display(sp); //
display("Visual FoxPro 6.0"); //
}

3
#include <iostream.h>
//int func(int num1 ,int num2 ,char ch = '+')
//int func(int num1 ,int num2 = 1,char ch = '+')
int func(int num1 = 1,int num2 = 1,char ch = '+')
{ int iRet = 0;
if (ch == '+')
{ iRet = num1 + num2;
}
if(ch =='*')
{ iRet = num1 * num2;
}
if(ch =='-')
{ iRet = num1 - num2;
}
if(ch =='/')
{ if(num2==0){cout<<" ";}
else{iRet = num1 / num2;}
}
return iRet;
}
void main()
{ cout << func(4,4,'*') << endl;
}
7 new delete

C++

data area
code area

stack area
heap area 3


1 C
malloc() free()
2 C++
C++
C C++ new delete

3 C++
C++ new
new

NULL

1 new [] //
int *ptr
ptr=new int // int

int * s; s = new int(200);
s int s
2 new [] //
char *ptr
ptr=new char [1024]// 1024 1KB

5
5

int *ptr=new int (5) int 5 ptr int


*ptr=new int [5] int 5 ptr

1#include <iostream.h>
void main()
{ int *b=new int(100);
int *array = new int[5];
for(int i=0;i<5;i++) array[i]=(i+1)*10;
for(int j=0;j<5;j++) cout<<array[j]<<, ;
cout<<\n b= <<*b<<endl;
delete b;
if (array != NULL)
{ delete []array;
array = NULL; }
}
: 10,20,30,40,50
b = 100
new
1
2
3

4 C++
C++ delete
delete

new
delete
;
new
delete ptr;

delete [];
array delete []array;

if (array != NULL)
{
delete []array;
array = NULL;
}
new delete C malloc()free()
void main()
{ int *ptr=new int; // int
*ptr= 1; // int
cout << *ptr;
delete ptr; // int
}

delete new delete


int X=1;
int *ptr1=&X;
int *ptr2=new int;
delete ptr1; // ptr2

----

void main()
{ int X=1; //
int *ptr=new int; //
*ptr=1;
delete ptr; // new
} // X

1#include <iostream.h>
void main()
{ int x; int * p;
p = &x;
int * q;
q = new int ;
*q = 100;
cout << p << endl;
cout << q << endl;
cout << &p << endl;
cout << &q << endl;
cout << "fish:" << *q << endl;
if (q != NULL) {
delete [] q; // delete q if
q = NULL; }
cout << "fish:" << *q << endl;
cout << &p << endl;
cout << &q << endl;
}
2#include<iostream.h>
void main()
{ int x=23;
int *p1=new int(5);
int *p2=new int(x-8);
cout<<*p1<< <<*p2<<endl;
int temp=*p1;
*p1=*p2;
*p2=temp;
cout<<*p1<< <<*p2<<endl;
delete p1;delete p2;
}

3#include<iomanip.h>
#include<iostream.h>
#include<stdlib.h>
const int n=12;
void main()
{ int *a=new int[n];
if(!a) // 0
{ cout<<<<endl;
exit(1); //
}
a[0]=0;a[1]=1;
for(int i=2;i<n;i++) a[i]=a[i-1]+a[i-2];
for(i=0;i<n;i+=){
cout <<setw(5)<<a[i];
if((i+1)%6==0) cout<<endl;
}
delete[]a;
}

. . C++
C++

C++
1

inline int add(int X, int Y)


{ return X+Y;
}
void main()
{ int Sum=add(1,2); // Sum=1+2 Sum=add(1,2)
}
2 inline

inline int add(int X, int Y)
{ return X+Y;
}
3
C++ C ,

#define Square(X) X*X X2 Square(X)


Y=Square(1+2)=1+2*1+2=5 9

inline int Square( int X)


{ return X*X;
}
Square(1+2)=Square(3)=9 9
4

2 3
#include <iostream.h>
inline void func(int a,int b)
{ cout<<"a * b = "<<a*b<<endl; }
main()
{ int x=10,y=3;
func(x,y); // a*b func
}

a * b = 30
inline
C++

5

inline int add(int X, int Y) void main(void)
{ return X+Y; { add(1,2);
}// }
void main(void) inline int add(int X, int Y)
{ add(1,2); // { return X+Y; //
} } //
6
switch

inline int add(int a, int b) //
{ if(a>b)
a=a+b;
return a;
}

1
#include<iostream.h>
inline double CalArea(double radius) //
{ return 3.14*radius*radius; }
int main()
{ double r=3.0; //r
double area;
area=CalArea(r);
// CalArea
cout<<area<<endl;
return 0;
}

. . C++
C++ &
1
alias



C++&

int X=1;
int & NewX=X; // NewX X
NewX=3; // X
//
cout<<X; //X 3 X NewX

2
&
&
int sumValue=1
int & sumData=sumValue

1 &
2
3
4 sumData sumValue

5

3
1
int &b; // b
2
int a=3;
int &b=a; // b a1
int &c=b; // c b
3

int a1,a2;
int &b=a1; // b a1
int &b=a2; // b a2
4
int X=1;
int & NewX=X;
NewX=3; // X
5
int a=3;
int &b=a;
cout<<&a<< <<&b<<endl; //a b
6
*&

#include<iostream.h>
void main()
{ int *p; int *&s=p;
int x=3; p=&x;
cout<<*p<<","<<*s;
}
7

char c[6]=Hello;
char &rc[6]=c; //
8


int a;
int &ra=a; //
int &*p=&ra; //
9 int &rp=NULL;
10 void void &ra=3;
11 const
int a=5;
const int &ra=a; // ra
ra=3; // ra
a=3; //




BC
int &d=120;

int temp=120;
int &d=temp; // temp
VC
int d=120;
const int &a=d+3; //, const

int tempd+3;
const int &a= temp;


BC
float f=5.6;
int &e=f;

int temp=int(f);
int &e=temp;
VC
float f=5.6;
const int &e=f;

int temp=int(f);
const int &e= temp;


#include <iostream.h>
void main()
{ int ival = 2048, a = 256, *pi = &a;
int &refVal = ival;
int *&refPi = pi;
ival ++;
cout << "refVal = " << refVal << endl;
refVal = a;
cout << "ival = " << ival<< endl;
*refPi = 1024;
cout << "a = " <<a<< endl;
}
4
C++

int add(int *X, int * Y) int add(int &X, int &Y)


{ *X=* X +*Y; { X=X+Y;
return *X; return X;
} }
void main() void main()
{ int a=1, b=2; { int a=1, b=2;
add(&a, &b); add(a, b);
} }


5

struct Data
{ int a[1000];
} AData;
void fun(struct Data X)
{ }

void fun(struct Data &X)
{ }
fun(AData)

----

C++
C++

int add(int x, int y)


{ return x+y ;
}
void main()
{ int a=1,b=2;
add(a,b); // a,b x,y
}

1 swap

int
#include<iostream.h>
void swap(int,int); /**/
void main()
{ int a,b;
cout<<" Input two data\n" ;
cin>>a>>b;
cout<<"a=<<a<< , <<"b=<<b<<endl;
swap(a,b); // a,b x,y
cout<<";
cout<<"a=<<a<< , <<"b=<<b<<endl;
}
void swap(int x,int y)
{ int temp;
cout<<"***x=<<x<< , <<"y=<<y<<endl;
temp=x;
x=y;
y=temp;
cout<<"!!!x=<<x<< , <<"y=<<y<<endl;
}
swap xy ab

Input two data


5 8
a=5 , b=8
***x=5 , y=8
!!!x=8 , y=5
a=5 , b=8


3


int add(int *x, int *y)


{ //C
return *x+ *y;
}
void main()
{ int a=1, b=2;
add(&a, &b);
}

1 swap
#include<iostream.h>
void swap(int*,int*); /**/
void main()
{ int a,b;
cout<<" Input two data\n" ;
cin>>a>>b;
cout<<"a=<<a<< , <<"b=<<b<<endl;
swap(&a,&b);
cout<<";
cout<<"a=<<a<< , <<"b=<<b<<endl;
}
void swap(int *x,int *y)
{ int temp;
cout<<"***x=<<*x<< , <<"y=<<*y<<endl;
temp=*x;
*x=*y;
*y=temp;
cout<<"!!!x=<<*x<< , <<"y=<<*y<<endl;
}

Input two data


5 8
a=5 , b=8
***x=5 , y=8
!!!x=8 , y=5
a=8 , b=5
:

return





int add(int &x, int &y)


{//C++
return x+y;
}
void main()
{ int a=1, b=2;
add(a, b);
}

1 swap
#include<iostream.h>
void swap(int&,int&); /**/
void main()
{ int a,b;
cout<<" Input two data\n" ;
cin>>a>>b;
cout<<"a=<<a<< , <<"b=<<b<<endl;
swap(a,b);
cout<<";
cout<<"a=<<a<< , <<"b=<<b<<endl;
}
void swap(int &x,int &y)
{ int temp;
cout<<"***x=<<x<< , <<"y=<<y<<endl;
temp=x;
x=y;
y=temp;
cout<<"!!!x=<<x<< , <<"y=<<y<<endl;
}

Input two data


5 8
a=5 , b=8
***x=5 , y=8
!!!x=8 , y=5
a=8 , b=5

*

6


void main(void)
{
int X=3;
int &refX=X;
fun(refX);
cout << X; //X 4
}
void fun( int & a)
{ a++; //a refX
}


int add(int &X, int &Y) // int add(int &X=1, int &Y=2) //
{ {
X=X+Y; X=X+Y;
return X; return X;
} }
7

int add(int &a, int & b) void main()


{ { int X=1, Y=2;
return a+ b; add(X,Y); //
} add(1,2); //
}
8


#include "iostream.h" // C++ C
void IncValue( int &X); // C++
void main()
{ int A=1;
IncValue(A);
cout <<"The Value of A Is:"<<A<<endl;
}
void IncValue(int &X)
{ X=X+1; // X main() A
}


#include iostream.h
void CalculateCircle(int R, float &CircleLen, float &CircleArea);
void main()
{
int circleR =10;
float Length, Area; //
CalculateCircle (circleR, Length, Area);
cout<<Circle Length=<<Length; //
cout<<Circle Area =<<Area;
}
void CalculateCircle (int R, float &CircleLen, float &CircleArea)
{
CircleArea=3.1415*R*R; // CircleArea Area
CircleLen=2*3.1415*R; // CircleLen Length
}

C++ return
C++


&
{
}
1 &

2


#include "iostream.h"
int & fun(int Num, int array[]) //
{ int & ref= array[Num]; //
return ref;
}
void main()
{ int A[5]={1,2,3,4,5}; // A[2] 3
fun(2,A)=55; // A[2] 55
cout << fun(2,A); // A[2] 55
}

1
#include "iostream.h"
void main()
{ int intOne;
intOne:5
int& rInt=intOne; rInt:5
intOne=5; &rIntOne:0xfff4
&rInt:0xfff4
cout<<"intOne:"<<intOne<<endl; intOne:8
cout<<"rInt:"<<rInt<<endl; intTwo:8
rInt:8
cout<<"&rIntOne:"<<&intOne<<endl; &intOne:0xfff4
cout<<"&rInt:"<<&rInt<<endl; &intTwo:0xfff2
&rInt:0xfff4
int intTwo=8;
rInt=intTwo;
cout<<"intOne:"<<intOne<<endl;
cout<<"intTwo:"<<intTwo<<endl;
cout<<"rInt:"<<rInt<<endl;
cout<<"&intOne:"<<&intOne<<endl;
cout<<"&intTwo:"<<&intTwo<<endl;
cout<<"&rInt:"<<&rInt<<endl;
}

2
#include <iostream.h>
void increment(int & value)
{ value++;
}
void main()
{ int i=2; increment(i);
cout << " i = " << i << endl;
}

3
#include <iostream.h>
void main()
{ const int n=2;
cout << " n = " << n << endl;
int &rn=n;
cout << " rn = " <<rn << endl;
rn+=10;
cout << " n = " << n << endl;
cout << " rn = " <<rn << endl;
}
n = 2
rn = 2
n = 2
rn = 12

const

strint(const char *str,int &val) 1 0


#include "iostream.h"
int strint(const char *str,int &val)
{ char c; val=0;
while(c=*str++)
if((c>=0)&&(c<=9))
val=val*10+c-0;
else return 0;
return 1;
}
void main(int argc,char *argv[])
{ int i,n;
for(i=1; i<argc;i++)
if(strint(argv[i],n))
cout<<Argv[<<i<<]=<<n<<endl;
else
cout<<Argument <<i<< is not a decimal<<endl;
}

5
#include "iostream.h"
float temp;
float fn1(float r)
{ temp=r*r*3.14;
return temp;
}
float &fn2(float r)
{ temp=r*r*3.14;
return temp;
}
void main()
{ float a=fn1(5.0);
//float &b=fn1(5.0);
float c=fn2(5.0);
float &d=fn2(5.0);
cout<<a<<endl;
//cout<<b<<endl;
cout<<c<<endl;
cout<<d<<endl;
}

6
#include "iostream.h"
float temp;
float & max(float x, float y)
{ if(x>y) temp=x; else temp=y;
return temp;
}
void main()
{ float a=max(10,20);
cout<<a=<<a<<,temp=<<temp<<endl;
max(10,20)=327.56; // max() temp
cout<<temp=<<temp<<endl;
}

a=20, temp=20
temp=327.56

7
float &fn2(float R)
{ float temp;
temp=R*R*3.14;
return temp;
}
void main()
{ float &d=fn2(5.0);//error
}
float &fn2(float R)
{ float temp;
temp=R*R*3.14;
return temp;
}
void main()
{ fn2(5.0)=12.4;// error
}


void Swap(int a, int b) //
{int t=a; a=b; b=t; }
void Swap(int &a, int &b) //
{int t=a; a=b; b=t; }
void Swap(int *a, int *b) //
{int t; t=*a; *a=*b; *b=t; }

1,

2, ,
,
3
:
:
1
2, , ,

. .
C++

C++ C++ C++



#include <>
#include

< >

" "

#include #include

#include <iostream.h>
#include "my_prog.cpp"
"iostream.h" C++
#include C++


#include "file2.h" file2.h
include
#include <file2.h> include
file2.h

#include <d:\oop\oop1\oop1.cpp>

C++
C++



#define

#define PI 3.1416 //

++



#define ONE 1
#define TWO ONE+ONE

1 C++
2

3
const
2

#define ADD(x, y) (x) + (y) //


:
:
C++
const

1
#define PI 3.1416 //
#define ADD(x, y) (x) + (y) //
#include <iostream.h>
void main()
{
int r = 4;
int a, b;
a = 10; b = 20;
cout << "" << PI * r * r << endl;
cout << "" << ADD(a , b) << endl;
}
3

#define MAX(a,b) ((a) > (b) ? \


(a) : (b)
4
#define REGISTER
REGISTER int i;
int i;



REGISTER
#define REGISTER register
register
5 # undef

#undef
# define PI 3.14159
main()
{
}
# undef PI/* PI */
f1()
PI main f1
6 # ##
##

#define string(a) #a
cout<<string(programming is fun);

cout<<"programming is fun";


#define concate(a,b) a##b
int xy=10;
cout<<concate(x,y);

cout<<xy;
####


/**/
3


1

#if 1 1
[#elif 2 2 ]

[#elif n n ]
[#else n+1]
#endif

1 1

C++#endif

1 1
#elif 2 2
#endif 2#elif
#else #endif #else
#endif
#if
#endif

1
#define DEBUG
#include <iostream.h>
void main()
{
#ifdef DEBUG
cout << " \n";
#endif
cout << "" <<endl;
}
2 #error
#error #error

#error

#define MYVAL 0 1 MYVAL

#if(MYVAL !=0 && MYVAL != 1)


#error MYVAL must be defined to either 0 or 1
#endif
3 defined
defined #if #elif

#define #undef
defined() defined
#if defined(mysym) #if defined mysym

#include <iostream.h>
#define DEBUG
void main()
{ int i=555;
// ....
#if definedDEBUG
cout<<"DEBUG: value of i is"<<i;
#endif
// ...
}
#define
defined #if defined

#if defined(mysym) && !defined(yoursym)

C++#ifndef
#ifdef
#define #ifndef #ifdef #ifndef
#ifdef C
defined

C++

. C++
C++

1 C

void main()
{ int x=1; //
for(int i=0; i<10; i++) //
{ }
}
2 C++
C++
---C++
for
void main(void) void main(void)
{ int x=1 // { for(int loop=1loop<10loop++)
{ int y // { int z=1 //
x=3y=4 }
} }
}
3 C++
C++

----
-----
-----
-----

-----


int X=1; //
void Function(int a) //
{ int b=a; //
X=b; //
Y=X; //
}
void main()
{ int Y=2; //
X=2; //
Function(X);
for(int i=0;i<10;i++) //
{ X=2; //
int C=0; //
}
X=2;Y=3; //
C=X; //
}
4 ::
1 C

#include "iostream.h"
int X=1;
void main()
{ int X=2;
cout <<X;
}
X 12 2 X

2 C++
C++
#include "iostream.h"
int X=1;
void main()
{ int X=2;
cout <<"local X=" << X; //
cout <<"global X=" <<::X; //::
}
3
::::
4
::
int X=1;
void main(void)
{ int X=2;
int Y=3;
::X=10; //
::Y=4; //
}
:: Microsoft SDK API VC++ MFC

C++
C++

C++C++
C
();

C++
();
int i=10;
float f=(float)i;
float f=float(i);
. void
void
void
void C++ void

void display()
{cout<<C++ Program<<endl;
}
2 void
void
C++ void

void display(void)
{cout<<C++ Program<<endl;
}

void
main()
main()
void main(void)
{ }
4 void

int a,b;
int *p1=&a,*p2=p1; //
int a;
int *p1=&a;
double *p2=p1; //, p1p2
void
C++void

void *p;

p
void C++ void void

int a;
int *p1=&a;
void *p2=p1; // int void
int *p3=p2; // void int
int *p4=(int *)p2; //void int

void
#include "iostream.h"
void main(void)
{ int a=1;
int *p1=&a, *p2=0;
void *p3=p1;
cout<<a=<<a<<endl;
cout<<*(int *)p3=<<*(int *)p3<<endl;//void
p2=(int *)p3; // int void
cout<<*p2=<<*p2<<endl;
}

a=1
*(int *)p3=1
*p2=1
.

double area (double radius);


double area(double length, double high);
area
area (// );

overload C++

1 C

int add(int X, int Y)


{ return X+Y;
}
float add(float X, float Y) //
{ return X+Y;
}
2 C++
C++
C++

int add(int X, int Y)


{ return X+Y;
}
float add(float X, float Y) //
{ return X+Y;
}
3
C

int intAdd(int X, int Y); int Add(int X, nt Y);


float floatAdd(float X, float Y); float Add(float X, float Y);
char charAdd(char X, char Y); char Add(char X, char Y);

4

C++

int add(int X, int Y); int add(int X, int Y);


float add(float X, float Y); int add(int X, int Y, int Z);

int add(int X, int Y); int add(int X, int Y);


int add(int a, int b); void add(int X, int Y);


int add(int X, int Y) float add(float X, float Y)
{ return X+Y; // { return X-Y; //
} } //
5
C++

class Person
{ void eat(Food bread )
{ }
void eat(Fruit apple )
{ }
}


int max(int x,int y)
int max(int x,int yint z=100)
max(5100)

1 int,long float 3

#include<iostream.h>
int Abs(int nn) // int
{ if(nn<0)
return(-nn);
else
return(nn);
}
long Abs(long ln) // long
{ if(ln<0)
return(-ln);
else
return(ln);
}
float Abs(float fn) // float
{ if(fn<0.0)
return(-fn);
else
return(fn);
}
void main()
{ int i=20;
long j=10000000L;
float p=-3.123;
cout<<integer i=<<Abs(i)<<endl;
cout<<long j=<<Abs(j)<<endl;
cout<<float p=<<Abs(p)<<endl;
}

2
#include <iostream.h>
int min(int a,int b);
int min(int a,int b,int c);
int min(int a,int b,int c,int d);
void main()
{ cout << min(13,5,4,9) << endl;
}
int min(int a , int b)
{ return a < b ? a : b;
}
int min(int a ,int b,int c)
{ int t = min( a , b );
return min( t , c );
}
int min(int a,int b,int c,int d)
{ int t1 = min( a , b , c );
return min( t1 , d );
}

3
#include<iostream.h>
#include<dos.h>
long GetTime(void) //
{ struct time t;
gettime(&t);
return (long)t.ti_hour*3600+ t.ti_min*60+ t.ti_sec;
}
long GetTime(int &hours,int &minutes,int &seconds)
{ struct time t;
gettime(&t);
hours= t.ti_hour;
minutes= t.ti_min;
seconds= t.ti_sec;
return (long)t.ti_hour*3600+ t.ti_min*60+ t.ti_sec;
}
void main()
{ int h,m,s;
cout<<Now is<< GetTime(h,m,s)<<seconds from midnight,;
cout<<Or<<h<<:<<m<<:<<s<<endl;
}

4
#include<iostream.h>
#include<string.h>
void sort(int num[],int count);
void sort(char *ch[],int count); // 2 sort()
void main()
{ char *city[]={Shanghai,Beijing,Tianjin,Qingdao,Chongqing};
int number[]={100,-20,25,-44,20};
sort(number,sizeof(number)/sizeof(int));
sort(city,sizeof(city)/sizeof(char *)); //
}
//
void sort(int num[],int count)
{ int i,j,k,t;
for (i=0;i<count-1;i++)
{ k=i;
for (j=i+1;j<=count-1;j++)
if(num[k]>num[j])
k=j;
if(k!=i)
{ t=num[k];num[k]=num[i];num[i]=t;
}
}
for (i=0;i<=count-1;i++)
cout<< num[i] <<\t;
cout<<endl;
}
void sort(char *ch[],int count)
{ int i,j,k;
char *t;
for (i=0;i<count-1;i++)
{ k=i;
for (j=i+1;j<=count-1;j++)
if(strcmp(ch[k],ch[j])>0)
k=j;
if(k!=i)
{ t=ch[k];ch[k]=ch[i];ch[i]=t; }
}
for (i=0;i<=count-1;i++)
cout<<ch[i] <<\t;
cout<<endl;
}
.
---C++ *&

1 *
*
* int X=1, y=2;
int Z=X*Y;
* int *Ptr=&X;
*
int X=1;
int *Ptr=&X;
*Ptr=3;
cout <<X; X 3
2 &
&
& int X=1, y=2;
int Z=X & Y;
&
int X=1;
int *Ptr=&X;
&
int X=1;
int &NewX=X;

1C++ C
main.cpp:
#include<iostream.h>
extern C long sum(int n);
void main()
{ int n;
cout<<Please input n:<<endl;
cin>>n;
cout<<sum=<<sum(n)<<endl;
}
sum.c
long sum(int n)
{ int j;
long s;
for(s=j=0;j<=n;j++) s+=j;
return s;
}

1main.cpp C++sum.c C
2 C++
ProjectOpen Project 1.prj
Project Add item, main.cpp sum.c

You might also like