c_cpp

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

Download "c_cpp"

Transcription

1 C C++ C C++ C++ (object oriented) C C++.cpp C C++ C C++ : for (int i=0;i<max; i++) string[i]=string2[i]; : c=a+b; int x; x=a-b; 1

2 C++ cout cin C++ #include <iostream> C printf scanf #include <stdio.h> C++ #include <stdio.h> printf scanf C++ : cout C++ cout cout << << cout << value ; cout << x; cout << Value <<x; cout << Value << x << \n ; cout << Value << x << \n ; cout << Value <<xx <<endl // endl 2

3 C++ : cin C++ cin cin >> >> cin >> answer; cin.getline(, ) cin.getline(str,80) ch=cin.get(); Namespace Namespace using C++ std namespace #include <iostream> using namespace std; 3

4 cin cout C C++ int, float, double, char signed, unsigned, long, short, const, volitale, static, register, extern C++ public, private, protected, friend, inline, virtual unsigned a unsigned int a short a short int a; long a long int a; 4

5 :: C (global variable) (local variable) C++ :: :: C++ :: :: :: ex1_1 int i=10; void main(){ int i=20; { cout<<"the inner block\n"; int n=i++; int i=30; ::i++; cout << i= <<i<< ::i= <<::i<<endl; cout << n= <<n<<endl; cout <<"The outer block\n"; cout << i= <<i<< ::i= <<::i<<endl; 5

6 : const const #define #define const const : const float pi= ; // #define pi // const int *p; (*p)++; : void a (const int *p) 6

7 ( ) C++ (reference) type & identifier1=identifier2; identifier1 identifier2 : int a,b; int &alt=a; // alt a alt=b; // a=b alt++; // a++ ( ) int a; int *point=&a; *point=a point=&a *point++; // a++ a++; // *point++; 7

8 char & refchar= \0 ; char temp= \0 ; char &refchar=temp; C (call by value) C & * C++ 8

9 : ex1_2 void Swap(int *p, int *q){ int temp; temp=*p; *p=*q; *q=temp; void main(){ int a,b; cout << a= ; cin >> a; cout << b= ; cin >> b; Swap(&a,&b); cout << a= <<a<<endl; cout << b= <<b<<endl; : ex1_3 void Swap(int &p, int &q){ int temp; temp=p; p=q; q=temp; cout << a= ; cin >> a; cout << b= ; cin >> b; Swap(a,b); cout << a= <<a<<endl; cout << b= <<b<<endl; void main(){ int a,b; 9

10 C++ : void abc(float k, int j, int i=5) // void xyz(float k=1,int j, int i) // void err_use(float k, int j=2, int i) // abc(5,3) abc(2,4,1) ex1_4 int add(int i,int j=1){ return(i+j); void main(){ int a; cout <<"a="; cin >> a; cout << add(a,2) << endl; //, a 2 cout << add(a) << endl; // 1, a 1 ( ) 10

11 inline C/C++ #define : #define sq(x) x*x #define C/C++ :(ex1_5) #define sq(x) x*x void main(){ int y=5; cout <<sq(y)<< \n ; // 25 cout <<sq(y+1)<< \n ; // 11, 5*1+5*1=11 inline #define C++ inline inline ( ) { // C++ inline inline : : 11

12 inline ex1_6 inline int SQ(int x){return (x*x); void main(){ int a=5,b=3; cout << SQ(a-b)= <<SQ(a-b); SQ(a-b)=4 C malloc, calloc, realloc( ), free #include <alloc.h> C++ new delete 12

13 new new : : = new ; float *buf; buf=new float; // 1 : = new [ ] ; int *p; p=new int[20]; // 20 delete delete new : : delete ; float *buf; buf=new float; // 1 : delete [] ; int *p; p=new int[20]; // 20 13

14 new malloc malloc new ( int, char, double, float) malloc free C (constructor ) (destructor) new delete C++ (constructor ) (destructor) new delete new delete ex1_7 #include <alloc.h> class a{ public: a(){ cout << "constructor by new\n ; ~a() {cout << "destructor by delete\n"; ; class b{ public: b(){ cout << "constructor by malloc\n"; ; void main(){ a *p; b *q; p=new a[3]; // 3 a q=(b *)malloc(sizeof(b)); // constructor delete [] p; free(q); constructor by new constructor by new constructor by new destructor by delete destructor by delete destructor by delete 14

15 C (global variable) (static variable) (local variable) ex1_8 void a(){ static c=0; c++; cout<<c<<endl; void main(){ a(); // 1 a(); // 2 15

16 C++ struct Stock { char type; double netto; static int count; ; Stock sk1,sk2; sk1.count sk2.count :: type :: public :: private public 16

17 ( ) a ctest ~ctest(){ 0 constructor 1 destructor 1 (ex1_9) class ctest{ private: static int a; public: ctest(){ a++; a--; void showa(){ cout << a <<endl; ; int ctest::a=0; void main() { ctest c1; c1.showa(); ctest c2; c2.showa(); /* 1 */ /* 2 */ ( ) ex1_10 class ptest{ private: static int a; public: float b; static int d; static void c(){ cout << here ; ; int ptest::d=0; int ptest::a=0; void main(){ ptest p,q; cout<< Size of class ptest= <<sizeof(ptest) << \n ; cout << start address of p= <<&p<< \n ; cout << address of p.b= <<&p.b<< \n ; cout << \ntest of static variable\n ; cout << address of ptest::d << \n ; cout << address of p.d= <<&p.d<< \n ; cout << address of q.d= <<&q.d<< \n ; cout << \nfunction in class\n ; cout << address of function c by &ptest::c= << &ptest::c<< \n ; cout << adress of function c by ptest::c= <<ptest::c<< \n ; cout << address of function p.c= <<p.c<< \n ; 17

18 cout << address of function q.c= <<q.c<< \n ; Size of class ptest=4 start address of p=0x1a130ffc address of p.b=0x1a130ffc Test of static variable address of ptest::d address of p.d=0x address of q.d=0x function in class address of function c by &ptest::c=0x e address of function c by ptest::c=0x e address of function p.c=0x e address of function q.c=0x e this : static ( ) 18

19 C++ : C (type) expression : y=(double)x; // x double y : type(expression) : y=double(x); // x double y static_cast static_cast<type to convert to>(object to convert) int z = 3; float x = static_cast<int>(z); dynamic_cast 19