ADVANCED OOP
OOP PRINCIPLES (PART 2)
This Photo by Unknown Author is licensed under CC BY-SA
int vectorSum(vector<int> nums){
return accumulate(nums.begin(), nums.end(), 0);
}
double vectorSum(vector<double> nums){
POLYMORPHISM return accumulate(nums.begin(), nums.end(), 0);
Principle of “one thing, many forms” }
Create functions and classes with
multiple variations int main() {
Remember: Templates facilitate vector<int> ints = {3,6,9,12,15,30};
polymorphism
vector<double> doubles = {1.1,2.2,4.4,8.8};
Runtime polymorphism cout<< vector Sum(ints) << endl;
cout<< vectorSum(doubles) << endl;
return 0;
}
ABSTRACTION
• Process of hiding internal details of class implementation
• Only essential details are shown
• Real-world example: driving a car
• We don’t need to see the engine or know exactly how the accelerator/brakes
work to be able to drive
• Abstraction is achieved in C++ using:
• Access modifiers
• Abstract classes
SPECIAL MEMBERS AND METHODS
C++ “THIS” POINTER
• Pointer included with every class class Student{
object private:
string name;
• Explicitly refers to the object double GPA;
calling the class method public:
Student(string name, double GPA){
• Implied by default when referring this->name = name;
to members and methods this->GPA = GPA;
• Use is recommended to: }
};
• Resolve scope conflicts
• Disambiguate code
• Chain calls to class methods
PURE VIRTUAL FUNCTIONS
• Virtual functions that are declared //Parent class - Shape
but not defined in parent class //Virtual method declaration.
virtual double area(void) = 0;
• Initialized with a value of zero
//Child class - Square
• Classes with at least one pure //Implementation of area method.
virtual method are abstract double area(void){
classes return this->width * this->height;
}
• Child classes override and provide
code
• If child class does not override
parent’s pure virtual methods, it
too is abstract
CONSTRUCTORS
• Called when an object of a class is created
• Arguments passed during object declaration are passed into the constructor
• Constructor name is the same as class name
• Establishes the initial state of the object, including values for member
variables
• Types of Constructor
• Default – Takes no arguments, sets members to fixed value
• Parameterized – Takes arguments that are used to initialize members
• Copy – Takes a reference to an object of the same class, copies member
values from reference object to calling object
COMMON CONSTRUCTOR TYPES
Default Parameterized Copy
//Constructor //Constructor //Constructor
Student(){ Student(string name, double Student(Student &other){
name = "Stu Dent"; GPA){ this->name = other.name;
GPA = 3.5; this->name = name; this->GPA = other.GPA;
} this->GPA = GPA; }
}
//Object Declaration //Object Declaration
Student s1; //Object Declaration Student s2(s1);
Student s1("Stu Dent", 3.5);
DESTRUCTORS
• Called when an object is manually deleted or goes out of scope
• Destroys the object and frees up memory
• Requires no arguments
• No return value
• Only one destructor defined in any given class
• Two ways to use the delete keyword (next slide)
OBJECTS WITH DYNAMIC DATA
• Allows us to initialize objects at runtime
• In OOP, this is achieved with parameterized constructors
• Key Use: Initializing dynamically-sized arrays
• new – dynamically initialize and allocate memory to object
• delete – dynamically delete object and release memory
• delete var Deletes dynamically allocated atomic value
• delete [] var Deletes dynamically allocated array
class DynArray{
private:
int *arr;
int size;
public:
DynArray(int size){
CREATING A this->size = size;
arr = new int[this->size];
DYNAMIC ARRAY for(int i=0; i<this->size; i++){
arr[i] = 1;
int main(){
}
DynArray newArray(16);
~DynArray(void){
cout<< newArray.toString() <<
endl; delete [] arr;
return 0; }
} string toString(void) const{
string message = "";
for(int i=0; i<size; i++){
message += to_string(arr[i]) + ", ";
}
return message;
}
};
STATIC MEMBERS AND METHODS
• Defined with static keyword
• Exist outside of class objects
• Can be accessed via directly referencing the class using the scope
resolution operator
• ClassName::staticMember
• ClassName::staticMethod()
• Static members have same value across class and all objects
• Static methods are recommended when accessing static data
class Student{
private:
string name;
double GPA;
public:
Student(string name, double GPA){
CONSTANT CLASS this->name = name;
this->GPA = GPA;
METHODS }
Defined with const keyword void setGPA(double newGPA){
GPA = newGPA;
Can access member variables, but }
cannot modify them
string getName(void) const{
Must use const keyword at the end of return name;
function header }
Using const at the beginning specifies double getGPA(void) const{
return type as a constant
return GPA;
Recommended: Declare as many }
methods const as possible to protect string toString(void) const {
data integrity return name ": " + to_string(GPA);
}
};
OPERATOR OVERLOADING
• Specifies how a class and operator class ToDoList{
interact private:
vector<string> list;
• Overload methods have a type like
public:
any method
ToDoList(){
• “operator” keyword followed by list.clear();
the operator being overloaded }
ToDoList& operator+=(string &newEntry)
• Most (not all) operators can be
{
overloaded
this->list.push_back(newEntry);
return *this;
}
};