10 C++ Hello!! C
C C++ C++ C++ 2
C++ 1980 Bell Bjarne Stroustrup C++ C C++ C, C++ C C 3
C C++ (prototype) (type checking) C C++ : C++ 4
C C++ (prototype) (type checking) [ 10-1] #include<stdio.h> extern type_c(); extern type_num1(double x); extern type_num2(const char*,int); int a,b; main(){ } type_c(); type_num1(a,b); type_num2(a,b); //C //C++ // : // : // : 5
C C++ [ 10-2] int max( int value1, int value2 ) return ((value1 > value2)? value1 : value2); C++ 6
C C++ [ 10-3] #include <math.h> extern double sin (double); int main(void){ double result; result = sin(60); return 0; // :result= sin(60.0) //c } 7
C C++ C++ ANSI C C C++ C++ C++ C C++ ANSI C void 8
C C++ (default) 9
C C++ (default) [ 10-4] #include <iostream.h> int max(int value1, int value2 = 0) { // value2 0 return ((value1 > value2)? value1 : value2); } int main(void) { int x,y; x = max(4, 3); cout << x << \n ; y = max(-2); cout << y << \n"; return 0; [ ] } 4 0 10
C C++ (enumeration) C++ (constant) const C C++ C int C++ (enumerator) 11
C C++ (8) (declaration extension) C++ C C++ C C++ 'struct' 'enum' 12
C++ (comment) C++ C ( /* */ ) ( // ) :, [ 10-5] int main(void){... y=3+x; /* C */ k=7+y; // C++... } 13
C++ (constant type) C++ C++ 'const' 'const' [ 10-6] #include<iostream.h> const char a='a'; // a const int b; // : const double c=1.48; int main(void){ //A cout<< a; c=2; // : return 0; } 14
C++ (constant type) [ 10-7] const int a=0; int *p = &a; // : *p++; [ 10-8] char *const str1 = "Hello"; // char const *str2 = "I am a boy"; // 15
C++ (reference type) C++ '&' '*' (alias), '&' [ 10-9] int a,b; int &pa =a; double &c=b; // : int &d=18; // : // // const int &e= 24; // // data // 16
C++ C++ (copy) C '*' [ 10-10] /* C : */ /* */ void replace_c(int *a,int *b) { int temp; /* */ temp=*a; /* a temp */ *a=*b; /* b a */ *b=temp; /* temp b */ } 17
C++ C++ C C++ '&' [ 10-11] //C++ : void replace_c++(int &a,int &b){ // int temp; // temp=a; // a temp a=b; // b a b=temp; //temp b } 18
C++ C '&' '*' [ 10-12] struct my_age{ int age=9; }; // my_age exp void R_my_age(const my_age& exp) { int x=exp.a; exp.a=0; // : } // 19
C++ new delete C++ 'new' 'delete' malloc new new sizeof [ 10-13] p=new int[size]; // size // delete p; // 20
C++ (scope operator) C C++ C++ '::' [ 10-14] #include<iostream.h> int a=10; // a int main(void){ int a=20; // a cout << "local variable a =" << a <<"\n"; // a cout <<"global variable a =" << :: a <<"\n"; // a return 0; } [ ] local variable a =20 global variable a =10 21
C++ (overload) C++ C++ [ 10-15] #include<iostream.h> #define PI 3.141592654 double area(double r){ // double double area; area=pi*r*r; return(area); } 22
C++ (overload) [ 10-15] ( ) float area(float r){ float area; area=pi*r*r; return(area); } int main(void){ float a,b; double c,d; a=23.54; c=23.54; b=area(a); cout<< b <<"\n"; d=area(c); cout<< d <<"\n"; return 0; } [ // float // //a area //a area ] 1740.86 1740.86 23
C++ (overload) C++ C++ '::', '.*', '.', '?:' [ 10-16] complex operator+(complex a,complex b) { return complex(a.real+b.real, a.imag+b.imag); } 24
C++ (inline) 25
C++ [ 10-17] #include<iostream.h> #include<stdio.h> #ifdef cplusplus inline int max(int a, int b){ return ((a > b)? a : b); } #endif int main(void) { int x=5; int y=6; int z; z = max(x, y); cout<<z; return 0; } [ ] 6 // max() // z=((x > y)? x : y) 26
C++ 'class' 'class' (data member) (member function) 27
C++ [ 10-18] class My_class{ private: //private static int my_private; // protected: //protect int my_protected; // protect public: //public static void Openpub(); // My_class(); // virtual ~My_class(); // virtual double My_const() const; //const virtual void My_virtual(); // }; // 28
C++ 'static' 'const' const 'virtual' 'friend' '~' 29
C++ (data member) (member function) C '::' ( ) 30
C++ 'private', 'protected', 'public' public public private protected private private protected public private 31
C++ C [ 10-19]... class My_class{...}; //My_class My_class object_1; //My_class object_1.my_virtual(); //object_1 My_virtual object_1.my_protected=1; //object_1 my_protected 1 My_class *object_2 = &object_1; // object_2 object_2 -> My_virtual(); // object_2 My_virtual() object_2 -> my_protected = 1; // object_2 my_protected 1 32
C++ '.' '.' '.' '->', 33
C++ (constructor) (destructor) '~' 1 1 34
C++ 2 35
C++ (friend) C++ 36
C++ [ 10-20] #include<iostream.h> class num2; // num2 class num1{ private: int number; public: friend int equal_or_not(num1 num1_ex, num2 num2_ex); void save_num(int a); }; class num2{ private: int number; public: friend int equal_or_not(num1 num1_ex, num2 num2_ex); void save_num(int a); }; 37
C++ [ 10-20] ( ) int equal_or_not(num1 num1_ex, num2 num2_ex){ // equal_or_not if(num1_ex.number == num2_ex.number) return 1; else return 0; } // number save_num void num1::save_num(int a){ number=a; } void num2::save_num(int a){ number=a; } 38
C++ [ 10-20] ( ) int main(void){ num1 num1_ex; num2 num2_ex; num1_ex.save_num(5); num2_ex.save_num(5); //private number 5 if(equal_or_not(num1_ex, num2_ex)==1) cout<< "equal\n"; else cout << "not equal"; return 0; } [ ] equal 39
C++ (member) 'static' '.', '->' '::' 40
C++ const const C++ int float 'const' const 'const' const const const const const const 41
(object oriented programming),, 42
(object) C++ 43
(polymorphism) C++ 2 44
(inheritance) 45
C++ [ 10-21] class college{ int college_prof; // int location; // public: int get_college_prof(int a); // int get_location(int a); }; // [ 10-22] class department : public college{ // int student; // int opened_class; // public: int get_student(int a); // int get_opened_class(int a);}; // 46
[ 10-23] #include<iostream.h> class college{ int college_prof; // int location; // public: int get_college_prof(int a); // int get_location(int a); // }; class department : public college{ // int student; // int opened_class; // public: int get_student(int a); // int get_opened_class(int a); // }; 47
[ 10-23] // int college::get_college_prof(int a){ college_prof=a; return college_prof; } int college::get_location(int a){ location=a; return location; } int department::get_student(int a){ student=a; return student; } int department::get_opened_class(int a){ opened_class=a; return opened_class; } 48
[ 10-23] int main(void){ department A; //department A int A_college_prof=10; int A_location=2; int A_student=200; int A_opened_class=15; // cout<<"professor="<<a.get_college_prof (A_college_prof)<<"\n"; cout<<"location="<<a.get_location (A_location) <<"\n"; cout<< "student=" <<A.get_student (A_student)<<"\n"; cout<< "Opened class="<<a.get_opened_class(a_opened_class); return 0; } [ ] professor=10 location=2 student=200 opened class=15 49
(base) (derived) [ 10-24] // Child_My_class::Child_My_class():My_class (){. }; // Child_My_class::~Child_My_class(){. }; 50
(base) (derived) 51
52 protected public private public private private
(multiple inheritance) [ 10-25] //A B public // Child_A_and_B class Child_A_and_B : public A, public B{... }; 53
: C++ C++. 54
(virtual function) 'virtual', 55
C++ C++ C [ 10-26] class Location{ // int X,Y; //2 public: Location(); Location(int x); //1 }; 56
C++ [ 10-27] // class Location{ int X,Y; //2 public: operator float_length() const; //Location float }; Location::operator float_length() const{ return (float)(x*x+y*y); } 57
this this 58
C++ this 59
[ 10-28] #include<iostream.h> class My_Plus{ public: int num; friend My_Plus operator+(my_plus object,int a); friend My_Plus operator+(int a,my_plus object); }; // + My_Plus operator+(my_plus object, int a){ My_Plus obj; obj.num = object.num + a; return obj; } 60
[ 10-28] // + My_Plus operator+( int a, My_Plus object){ My_Plus obj; obj.num = object.num + a; return obj; } int main(void){ My_Plus Apple; Apple.num =3; Apple = 2 + Apple; Apple = Apple + 5; cout << Apple.num; return 0; } [ ] 10 61