1 / 10

Programming with C++

Programming with C++. Woochang Lim. C+OOP = C++ C (non OOP)  C++ (non OOP+OOP)  Java (OOP) Object-Oriented Design  Object-Oriented Programming. Generic Program in C++. C++: Object-Oriented version of C C language + OOP = C Generic program in C = Generic program in C++

Download Presentation

Programming with C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming with C++ Woochang Lim C+OOP = C++ C (non OOP)  C++ (non OOP+OOP)  Java (OOP) Object-Oriented Design  Object-Oriented Programming SNPL

  2. Generic Program in C++ C++: Object-Oriented version of C C language + OOP = C Generic program in C = Generic program in C++ Basic statements in C can be used in C++. - Declaration of variables and functions e.g. Global variables and local variables - Flow-control statements e.g. if, if … else, while, do … while - Screen and file I/O e.g. printf, scanf, fprintf, scanf - Operators, format specifiers, escape sequences, etc. e.g. &&(AND), ||(OR), etc. For the C programmer, C++ is easy to use. SNPL

  3. C++, Useful Programming Language // This is an example #include <iostream.h> typedef unsigned short int USHORT int main() { USHORT Width; USHORT Length=10; USHORT Area=Width*Length; cout << “Enter the width:”; cin >> Width; cout << “Width:” << Width << “\n”; cout << “Length:” << Length << endl; cout << “Area:” << Area << “\n”; } //: Comments cout: Print output to screen cint: Input from the keyboard typedef: Define a new variable type You can use C++ as non-OOP language. When your program is not designed for the object-oriented concept, you are doing non-object-oriented programming although you are using the object-oriented languages, C++. SNPL

  4. Classes Class: Member variables (data members) + Member functions (methods) Declaring a class class Cat { unsigned int itsAge; unsigned int itsWeight; Meow(); }; Defining an object: An object is an instance of the class. Cat Frisky; Accessing the class member Frisky.itsAge=5; Frisky.itsWeight=6; Frisky.Meow(); SNPL

  5. Public and Private All members of classes are private by default. Private members can be accessed through the methods in the classes. Public members can be accessed by other objects class Cat { public: // Public methods unsigned int GetAge(); void SetAge(unsigned int Age); unsigned int GetWeight(); void SetWeight(unsigned int Weight); // Public member function Meow(); private: unsigned int itsAge; unsigned int itsWeight; }; Private members: Security for the programming error SNPL

  6. Implementing Class Method // Implementing class method #include <iostream.h> // Declare a class Cat class Cat { public: int GetAge(); void SetAge(int age); void Meow(); Private: int itsAge; }; // Define a public method GetAge() int Cat::GetAge() { return itsAge; }; // Define a public method SetAge() int Cat::SetAge(age) { itsAge=age; }; // Define a public method Meow() // Print out “Meow” void Cat::Meow() { cout << “Meow.\n”; }; int main() { // Define a object Frisky Cat Frisky; // Set the Frisky’s age Frisky.SetAge(5); // Call the public member function Meow() Frisky.Meow(); cout << “Frisky is a cat who is” << Frisky.GetAge() << “ years old.\n”; Frisky.Meow(); return 0; } SNPL

  7. Constructor and Destructor // Implementing class method #include <iostream.h> class Cat { public: Cat(int initialAge); // Constructor ~Cat(); // Destructor int GetAge(); void SetAge(int age); void Meow(); Private: int itsAge; }; // Constructor Cat::Cat(int initialAge) { itsAge = initialAge; }; // Destructor Cat::~Cat() { }; int Cat::GetAge() { return itsAge; }; int Cat::SetAge(age) { itsAge=age; }; void Cat::Meow() { cout << “Meow.\n”; }; int main() { Cat Frisky(5); Frisky.Meow(); cout << “Frisky is a cat who is” << Frisky.GetAge() << “ years old.\n”; Frisky.Meow(); Frisky.SetAge(7); cout << “Frisky is a cat who is” << Frisky.GetAge() << “ years old.\n”; return 0; } SNPL

  8. Inheritance Inheritance Multiple Inheritance class animal { public: void eat(void); void sleep(void); protected: void breathe(void); }; class elephant: public animal { public: void trumpet(void); }; class animal { public: void eat(void); void sleep(void); protected: void breathe(void); }; class terrestrial { public: void walk(void); }; class elephant: public animal, public terrestrial { public: void trumpet(void); }; SNPL

  9. Polymorphism Function overloading: Compile-time polymorphism Same function names, but various operations #include <iostream.h> #include <conio.h> class printer_type { public: void printem(char the_char); void printem(char* the_string); void printem(int the_ASCII_code); }; void printer_type::printem(char the_char) { cout << the_char; } void printer_type::printem(int the_ASCII_code) { cout << (char) the_ASCII_code; } void printer_type::printem(char* the_string) { cout << the_string; } int main() { printer_type my_printer; my_printer.printem(97); // ASCII code for ‘a’ my_printer.printem(‘b’); // Char my_printer.printem(“c”); // String cout << endl; return 0; } SNPL

  10. More Information for The OOP in C++ Books 1. S. Holzner, C++ Programming, Prentice Hall 1993. 2. J. Liberty, SAMS Teach Yourself C++ in 21 Days: Complete Compiler Edition, SAMS 1999. Home Pages 1. C/C++ Language Guide http://kitel.co.kr/~wanwan/ 2. Standard C++ Library http://oopsla.snu.ac.kr/~sjjung/stl/ SNPL

More Related