180 likes | 340 Views
Use Classes with Object-Oriented Programming in C++. By Wayne Cheng. Object-Oriented Programming. Introduction Five Tenets Terminology The foundation of C++: Classes. Introduction. It is the name given to a specific paradigm in the field of programming.
E N D
Use Classes with Object-Oriented Programming in C++ By Wayne Cheng
Object-Oriented Programming • Introduction • Five Tenets • Terminology • The foundation of C++: Classes
Introduction • It is the name given to a specific paradigm in the field of programming. • With objects we are creating pieces of code that can stand alone. • You know the box has inputs and outputs but you don’t know what is inside it.
Five Tenets • Encapsulation • Data Abstraction • Information Hiding • Inheritance • Polymorphism
Encapsulation • The goal is to bind data and the functions that operate on that data together in one “object”. • The functions will be our interface while the data will be hidden in the black box. • Classes in C++ bind data and functions together in objects.
Data Abstraction • The goal is to force users to interface with the object’s functions rather than accessing data directly. • Data is in the box. • The user can not access the data, he/she must use the defined interface functions to manipulate the data. • Public: elements of class are accessible from outside of class. • Private: restricts access to these functions and data members to this class alone. (as default) • Protected: restricts access to these functions and data member to this class and its children. (Children are classes derived from the current class)
Information Hiding • The goal is to prevent the user from seeing how our functions are implemented and exactly what they do. • All the user will see are function prototypes, but no actual implementation. • Multiple file projects: by storing each class implementation in its own file, we can compile and distribute them without allowing users to see the internals of functions.
C++ Class definition The syntax for a class definition is • class Class_Name{ public: Member_Specification 1 Member_Specification 2 … Member_Specification 3 private: Member_Specification_n+1 Member_Specification_n+2 …};
Inheritance • It is a mechanism that allows us to include “Parent” class into a “Child” class. • It just another name for the topic of derived classes. • The goal is code reusability and extensibility. • With it we can reuse code without having to cut and paste it out of our old programs. • It is a major stepping stone on the path to polymorphism.
Polymorphism • The goal is to write generic code that can handle multiple objects in the same way. • In code, we can pass multiple objects all as the same base object. • It is refers to the ability to associate multiple meaning to one function name by means of a special mechanism known as “Late Binding”. • We use class hierarchies to allow us to treat child classes as their parent class or grandparent, etc.
Terminology • Object – A collection of related data and functions bound together. • Method – A function of an object. • Message – A call to an object method. The term “message passing” in reference to OOP you are dealing with method calls. • Member – A method or variable that is part of an object (or class). • Association – an object using another object that exists outside of the first. (Must pass by address or reference) • Aggregation – An object that contains another object. • Generalization – An object that publicly inherits its parent. (Inheritance) • Instance – variable of data type (class or struct).
The foundation of C++: Classes • The class is how we are able to implement OOP concepts in C++. • The class by itself gives us three of our five tenets OOP such are Encapsulation, Data Abstraction and Information Hiding. • It is a technique used in binding functions to the data that they manipulate. (Encapsulation) • It allows us to protect the data from the users direct manipulation. (Data Abstraction) • It allows us to hide the implementation of functions from the user. (Information Hiding) • It defines all its members to be private by default.
Classes • A class is a data type whose variables are objects • The definition of a class includes - Description of the kinds of values of the member variables - Description of the member functions • A class description is somewhat like a structure definition plus the member variables
C++ Class Example • Examine a stack class: two functions and two pieces of data. Class: stack Functions • “Push”: it will have one parameter and will return a Boolean value indicating success or failure. The data passed in will be placed at the into the stack at the top. • “Pop”: it will have one parameter, a reference, and will return a Boolean value indicating success or failure. The value of the reference will be set to the value at the top of the stack. Data: • The first element of data will be the stack itself. • The second element of data will be called top and will mark the current position in the stack.
C++ Class Code ///////////////////////////////////////////////////// //File IntStack.h // This is an integer stack class. ///////////////////////////////////////////////////// class IntStack { public: IntStack(); //the class constructors int push(int); //the push function prototype int pop(int&); //the pop function prototype private: int stack[10], top; //the definition of calss variables }
Class Code /////////////////////////////////////////////// //File: IntStack.cpp /////////////////////////////////////////////// #include “IntStack.h” //Import class specification IntStack::IntStack() //The constructor { top = -1; } int IntStack::push(int x) //The push function implementation { if(top == 9) return 0; top++; stack[top] = x; return 1; } int IntStack::pop(int & x) //The pop function implementation { if(top == -1) return 0; x = stack[top]; top--; return 1; }
Class Code ///////////////////////////////////////////// //pgm.cpp ///////////////////////////////////////////// #include “IntStack.h” #include <iostream.h> void main ( ) { IntStack stack; int back; for (int x = 0; x < 12; x++) { cout << “Pushing “ << x << “…”; cout << (stack.push(x)?”Success” : ”Fail”) << endl; } for (x = 0; x < 12; x++) { cout << “Poping…”; (stack.pop(back)? (cout << back) : (cout << “Fail”)) << endl; } }