1 / 16

Object-Oriented Programming: Encapsulation and Inheritance Example

This example demonstrates encapsulation and inheritance in object-oriented programming through a commission employee class. It also covers strings and formatting in C++/CLI.

moodym
Download Presentation

Object-Oriented Programming: Encapsulation and Inheritance Example

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. outline • Class and Object-Oriented Programing • Encapsulation and inheritance • Example • Commission Employee • Encapsulation and inheritance • Strings and Formatting • In-class assignment • Derived class • Access scope

  2. Class • Procedural vs Object-oriented • User-defined types (programmer-defined types) • class • Encapsulation (private and public) • Member Variables and access modifiers • Member Methods • Constructor • Constructor and method overloading • Method overloading: two or more methods have same name but with different number or type of parameters. • Using class

  3. ref class Square { public: Square (int d) { Dims = d; } int Area () { return Dims*Dims; } int Dims; };

  4. Use ref class • Square ^sqr1 = gcnew Square(); // a handle sqr1->Dims = 5; int area = sqr1->Area (); • Or: • Square sqr2; // a local or a stack instance sqr2.Dims = 3; int i = sqr2.Dims; int area = sqr2.Area ();

  5. Example CommissionEmployee • firstName • lastName • socialSecurityNumber • grossSales • commissionRate • Read/write these variables • getXXX() / setXXX() • earnings() • print() private and public accesses

  6. Constructor • Initialize the object at instantiate • A special method: called at object creation by gcnew(), same name as the class, no return (not defined with void either), can take any number of parameters, public • Constructor overloading class testclass { public: testclass () { x = y = 0; } testclass(Int32 a) { x = y = a; } // x =a; y = 10; testclass(Int32 a, Int32 b) { x = a; y = b; } private: Int32 x; Int32 y; };

  7. testclass ^a1 = gcnew testclass(100); testclass ^a2 = gcnew testclass(); testclass ^a3 = gcnew testclass(100, 2);

  8. static class methods and variables • Static variable in a class: • only one copy of the variable is created, and it is shared by all the instances of the class. (not associated with an instance) • Initiated only at the first time the class is instantiated. ref class staticVar { static int counts = 3; }; • Static method in a class: • the method is accessible without the need to instantiate the class. • Use only static member variables. Public: static int get_x () { return x; } • To use it: Console::WriteLine(className::get_x());

  9. Inheritance • Code reuse • Base class and derived class in C++ • this and class scope(::) in C++ • Method overriding • A method in the derived ref class has an identical signature to the base ref class • Access modifiers • protected

  10. using namespace System; // Base class ref class Square { public: int Area () { return Dims*Dims;} int Dims; }; // Derived class Ref class Cube : public Square { public: int Volume() { return Area()* Dims; } };

  11. // inheritance in action void main(void) { Cube ^cube = gcnew Cube (); cube->Dims = 3; Console::WriteLine(cube->Dims); Console::WriteLine(cube->Area()); Console::WriteLine(cube->Volume()); }

  12. Access modifiers • Public • Accessible by external functions and methods • Accessible to derived ref classes • Private • Not accessible by external functions and methods • Not accessible to derived ref classes • Protected • Not accessible by external functions and methods • Accessible to derived ref classes

  13. Example • firstName • lastName • socialSecurityNumber • grossSales • commissionRate • earnings() • print() CommissionEmployee • baseSalary • earnings() • print() BasePlusCommissionEmployee

  14. Strings and Formatting in C++/CLI • http://msdn.microsoft.com/en-us/library/system.string.aspx • public: static String^ Format( String^ format, ... array<Object^>^ args ) • Format:{index[,length][:formatString]} • {0}, {0, 10}, {0, -10}, {0, -10:D6}, {0,-10:f} double d = 134; int a = 30; Console::WriteLine("{0,-10:f}{1,-10}",d, a);

  15. String^ dateString1 = "1"; String^ fmtString = String::Format("{0,10}",dateString1); // -10 to left, 10 to right Console::WriteLine(fmtString); int dateString2 = 1; String^ fmtString = String::Format("{0,-10:D6}",dateString2); Console::WriteLine(fmtString); String^ result = nullptr; result += String::Format("{0,10}", L"X4"); result += String::Format("{0, 10:D6}", dateString2); result += String::Format("{0,10}", L"end"); Console::WriteLine(result);

  16. String^ result1 = L"X4"; String^ result2 = String::Format("{0} ", dateString2); String^ result3 = L"end"; Console::WriteLine("{0,10} {1, 10} {2,10}", result1, result2, result3); fmtString = String::Format("{0,10} {1, 10} {2,10}", result1, result2, result3); Console::WriteLine(fmtString); fmtString = String::Format("{0,10} {1, 10} {2,10}", result1, dateString1, result3); Console::WriteLine(fmtString);

More Related