A Basic of C Basic Structure of C
A Basic of C++
Basic Structure of C++ /* c++파일 확장명 filename: Hello. cpp */ #include<iostream. h> void main() { cout<<“Hello world n”; } 프로그램이 처음 시작할 때 자동으로 호출 된다 함수 프로그램구조 순서 1. #inclue<iostream. h> 2. 원형함수 3. main()함수 4. 정의 함수 #include<iostream. h> int Find. Area(int x); // 원형 함수 void main() { 변수 지정; // int a=10; 함수 호출; // Find. Area(10); } int Find. Area(int x) // 정의 함수 { 문장내용 retrurn 반환 값; }
Variable 변수란 : 정보를 저장하는 장소이며, 값들을 저장하고 그 값들을 검색하는 컴퓨터의 메모리의 특정 위치이다. unsigned : 항상 양수를 갖는다. signed : unsigned 가 붙지 않은 정수형으로 음수 및 양수를 가질 수 있다. short int 2 bytes -32, 768~32, 762 long int 4 bytes -2, 147, 483, 648~2, 147, 483, 647 Int 4 bytes -2, 147, 483, 648~2, 147, 483, 347 char 1 bytes 256 문자 값들 float 4 bytes 1. 2 e-38~ 3. 4 e 38 double 8 bytes 2. 2 e-308~1. 8 e 308
The Basis of Class 4. #include<iostream. h> Class Cat { private: // 전용 접근 제한자 int its. Age; public: // 범용 접근 제한자 Cat(int initaial. Age); ~Cat(); int Get. Age(){return its. Age; } // inline void Set. Age(int age); void Meow(){cout<<“Meow”; } }; Cat: : Cat(int inital. Age) // 생성자 { its. Age= initial. Age; } Cat: : ~Cat() //소멸자 { } Cat: : Set. Age(int age) // 클래스 메소드 { its. Age = age; } void main() { Cat Frisky(5); // 객체를 생성, 생성자 함수 호출 Frisky. Meow(); //객체의 함수호출 cout<<“Frisky is a cat who is “; cout<<Frisky. Get. Age()<<“year”; Frisky. Mewo(); Frisky. Set. Age(7); cout<<“Now Frisky is”; cout<<Frisk. Get. Age()<<“Year”; }
Pointer Example #inclue<iostream. h> class Simple. Cat {private: // 전용접근 제한자 int *its. Age; //정수형 포인터 int *its. Weight; public: // 범용접근 제한자 Simple. Cat(); // 생성자 ~Simple. Cat(); // 소멸자 int Get. Age()const{return *its. Age; } void Set. Age(int age); int Get. Weight( )const; }; Simple. Cat: : Simple. Cat() { its. Age = new int(2); its. Weight= new int(5); } Simple. Cat: : ~Simple. Cat() { delete its. Age; delete its. Weight; } Simple. Cat: : Set. Age(int age) { *its. Age = age; //포인터에 age를 할당한다. } Simple. Cat: : Get. Weight()const { return *its. Weight; //포인터를 반환 값으로 가진다 } void main() { Simple. Cat *Frisky = new Simple. Cat; //객체포인터 cout<<(*Frisky). Get. Age(); //포인터를 사용한 함수 호출 1 Frisky –>Set. Age(5); //포인터를 사용한 함수 호출 2 cout<<Frisky –>Get. Age(); delete Frisky; }
Reference Example 1 #include<iostream. h> class Smple. Cat { public: // 범용접근 제한자 Simple. Cat(int age, int weight); ~Simple. Ct(){} int Get. Age( ){return its. Age; }//inline int Get. Weight( ); private: // 전용접근 제한자 int its. Age; int its. Weight; }; Simple: : Simple. Cat(int age, int weight) { its. Age = age; its. Weight = weight; } int Simple. Cat: : Get. Weight() { return its. Weight; } void main { Simple. Cat Frisky(5, 8); // 객체변수를 r. Cat 참조자로 선언 Simple. Cat &r. Cat = Frisky; cout<<“Frisky is: ”; cout<<Frisky. Get. Age()<<“years old. n”; cout<<“And Frisky weights: ”; // < 참조자를 사용하여 함수를 호출> cout<<r. Cat. Get. Weight()<<“Pounds. n”; }
Reference Example 2 #include<iostream. h> // <반환 값 없는 원형 함수> void swap(int &x, int &y); void main() { int x=5, y=10; cout<<“main. before swap x: ”; cout<<x<<“y: ”<<y<<“n; swap(x, y); // 함수를 호출 //참조자에 의해 x, y 값이 달라진다. cout<<“r. X : ”<<x<<“r. Y : ”; cout<<y<<“n”; } // <인수를 정수형 참조자로 선언했다. > void swap(int &r. X, int &r. Y) { int temp; cout<<“swap. Before swap. r. X: ”<<r. X; cout<<“r. Y: ”<<r. Y<<“n”; temp =r. X; r. X =r. Y; r. Y =temp; cout<<swap. After swap, r. X: ”<<r. X; cout<<“r. Y: ”<<r. Y<<“n”; }
Inheritance Example 1 #include<iostream. h> class Vehicle // 상위클래스 { public: // 범용 접근 제한자 void start(){cout<<“시동…n”; } void move(){cout<<“조종. . n”; } }; // <상속 받은 클래스= 하위클래스> class helicopter: public Vehicle { public: void move(); } void helicpter: : move() { cout<<“비행…”<<endl; } void main() { helicopter whily; //객체를 정의 // 상위클래스의 함수를 호출할 수 있다. whily. start(); // 객체의 함수 호출 whily. move(); } // <실행 결과> /* 시동… 비행… */
Inheritance Example 2 #include<iostream. h> enum BREED{YORKIE, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB}; class Mammal // 상위 클래스 { public: // 범용 접근 제한자 Mammal(); its. Age(2), its. Weight(5){} ~Mammal(){} int Get. Age()const{return its. Age; } void Set. Age(int age){ its. Age =age; } int Get. Weight()const{return its. Weight; } void Set. Weight(int weight){its. Weight = weight; } void Speak() const{cout<<“mammal sound!”; } void Sleep()const{cout<<“I’msleepn”; } protected: //전용 접근 제한자 int its. Age; int its. Weight; }; class Dog: public Mammal //상속 받은 하위클래스 { public: Dog(): its. Breed(YORKIE){} ~Dog(){} BREED Get. Breed()const{ return its. Breed; } void Set. Breed(BREED breed){ its. Breed = breed; } void Wag. Tail(){cout<<“Tail wagging…n”; } void Beg. For. Food(){cout<<“Begging for food. . n”; } private: BREED its. Breed; }; int main() { Dog fido; fido. Speak(); // 상위클래스의 함수 호출 fido. Wag. Tail(); cout<<“Fido is”<<fido. Get. Age()<<“years oldn”; }
Polymorphism Example 1 #include<iostream. h> Class Horse{ public: // 범용접근 제한자 Horse(){cout<<“horse constructor. . ”; } virtual ~Horse(){cout<<“Horse destructor. . n”} virtual void Whinny()const{cout<<“Whinny!. ”; } private: // 전용접근 제한자 int its. Age; }; class Bird{ public: Bird(){cout<<“Bird condstuctor. . ”; } virtual ~Bird(){cout<<“bird destruntor”; } virtual void chirp()const{cout<<“chirp. . ”; } virtual void Fly()const{Cout<<“I can Fly!”; } private: int its. Weight; }; class Pegasus: public Horse, public Bird { // 다중 상속 public: void Chirp()const{Whinny(); } Pegasus(){cout<<“Pegasus constructor. . ”; } ~Pegasus(){cout<<“Pegasus destructor. . ”; } }; const int Magic. Number =2; void main() { Horse * Ranch[Magic. Number]; Bird* Aviary[Magic. Number]; Horse * p. Horse; Bird* p. Bird; int choice; for(int i = 0 ; i<Magic. Number ; i++) { cout<<“n(1)Horse(2)Pegasus: ”;
Polymorphism Example 1 cin>>choice; cout<<“n”; if(choice = = 2) p. Horse = new Pegasus; else p. Horse = new Horse; Ranch[i] = p. Horse; } for(i=0 ; i<Magic. Number ; i++) { cout<<“n(1)Bird(2)Pegasus: ”; cin>>choice; if(choice = = 2) p. Bird = new Pegasus; else p. Bird = new Bird; Aviary[i] = p. Bird; } for(i=0 ; i<Magic. Number ; i++) { cout<<“n Ranch [ “<< i <<“ ]: ”; Ranch[I]->Whnny(); delete Ranch[i]; } for(i=0 ; i<Magic. Number ; i++) { cout<<“n Aviary [“ << i << “ ]: ” ; Aviary[i]->Chirp(); Aviary[i]->fly(); delete Aviary[i]; } }
Static Member Data , Static Function Example #include<iostream. h> Class Cat { public: // 범용 접근 제한자 Cat(int age): its. Age(age){How. Many. Cats++; } virtual ~Cat(){How. Many. Cat--; } virtual int Get. Age(){return its. Age; } virtual void Set. Age(int age){ its. Age=age; } // <정적 멤버 함수> static int Get. How. Many(){return How. Many. Cats; } private: // 범용 접근 제한자 int its. Age; static int How. Many. Cats; // 정적 멤버 변수 }; int Cat: : How. Many. Cats =0; void Telepathic. Funciton(); //원형 함수선언 void main() { const *Cat. House[Max. Cats]; for(int I=0; I<Max. Cats; I++) { Cat. House[I] =new Cat(I+2); Telepathic. Function(); } for(I=0; I<Max. Cats; I++) { delete Cat. House[I]; Telepathic. Function(); Cat. House[I] =0; } } void Telepathic. Funtion() //정의 함수 { cout<<Cat: : Get. How. Many(); }
- Slides: 20