cpp_oop - IEG4180 Network Software Design and Programming...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: IEG4180 Network Software Design and Programming Supplementary Notes on C++ Programming Part II - Object-Oriented Programming IEG4180 : Supplementary Notes on C++ Programming - Part 2: OOP 2 Copyright Jack Y.B. Lee 0. Contents • 7. C++ Class • 8. Class Members • 9. Constructors • 10. Destructors • 11. Member Initialization • 12. Static Members • 13. Copying Class Objects • 14. Constant Member Functions • 15. Self-Reference • 16. Structures and Classes • 17. Nested Class IEG4180 : Supplementary Notes on C++ Programming - Part 2: OOP 3 Copyright Jack Y.B. Lee 0. Contents • 18. Operator Overloading • 19. Derived Classes • 20. Abstract Classes • 21. Multiple Inheritance • 22. Class Templates • 23. Function Templates • 24. Inheritance and Templates IEG4180 : Supplementary Notes on C++ Programming - Part 2: OOP 4 Copyright Jack Y.B. Lee 7. C++ Class • W h a t ? ¡ A language construct to allow the creation of new types that can be used as conveniently as the built-in types. • How? ¡ Creating a new Date class: class Date { public: void init(int dd, int mm, int yy); // initialize void add_year(int n); // add n years void add_month(int n); // add n months void add_day(int n); // add n days private: int d, m, y; }; Functions within a class are called member functions (methods in generic terms). The public keyword specifies that the following declarations are accessible to all. The private keyword specifies that the following declarations are accessible ONLY to member functions of the same class. Access within a class is by-default private . date.h IEG4180 : Supplementary Notes on C++ Programming - Part 2: OOP 5 Copyright Jack Y.B. Lee 7. C++ Class • Encapsulation ¡ Principles: • There are three basic levels of access to a class's member: – Public, Protected, and Private • There are four scopes in accessing a class's member: – Class Scope, Derived Class, Friends, and External Scope ¡ Comparison: Access Control Matrix Public Protected Private Class Scope Friends Derived Class External Scope Yes Yes Yes Yes Yes Yes Yes No Yes Yes No No IEG4180 : Supplementary Notes on C++ Programming - Part 2: OOP 6 Copyright Jack Y.B. Lee 8. Class Members • Defining Class Member Functions • Accessing Class Members void Date::init(int dd, int mm, int yy) // initialize { d = dd; m = mm; y = yy; } void Date::add_year(int n) // add n years { y += n; } date.cpp void timewarp(Date &d) { d.y -= 200; // error, Date::y is private. d.add_year(-200); // okay, Date::add_year is public. } test.cpp • Unlike C, we can now make something visible (to the user) but not accessible in C++ by declaring them private in a class. • Declarations in the header file date.h is the interface of the Date class....
View Full Document

Page1 / 73

cpp_oop - IEG4180 Network Software Design and Programming...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online