DSA6 OOP Concepts
DSA6 OOP Concepts
MARGIE'S TRAVEL
Data Structures
and Algorithms
T- C P E T 1 2 1
M
2
Constructor
Encapsulation
Static Members
MARGIE'S TRAVEL
Inheritance
Abstraction
M
4
Constructor
as a special type of function with the
same name as the class.
a void method
MARGIE'S TRAVEL
M
5
CONSTRUCTOR
#1: Implement Constructor in C++ Class files
#include <iostream> //Header File File //CPP File
using namespace std;
#include "Tester.h" #ifndef TESTER_H #include "Tester.h"
#define TESTER_H #include <iostream>
int main() #include <iostream> using namespace std;
{ using namespace std;
Tester X; Tester::Tester(){
return 0; class Tester cout<<"I am an constructor";
} { }
public:
MARGIE'S TRAVEL
Tester();
protected:
private:
};
#endif // TESTER_H
M
6
CONSTRUCTOR
#2: Implement Constructor in C++ Class files (with parameters)
#include <iostream> //Header File File //CPP File
using namespace std;
#include "Tester.h" #ifndef TESTER_H #include "Tester.h"
#define TESTER_H #include <iostream>
int main() #include <iostream> using namespace std;
{ using namespace std;
Tester X(2,3); Tester::Tester(int A,int B){
return 0; class Tester cout<<A + B;
} { }
public:
MARGIE'S TRAVEL
Tester(int,int);
protected:
private:
};
#endif // TESTER_H
M
7
Encapsulation
binds together the data and functions
that manipulate the data, and that keeps
both safe from outside interference and
misuse.
MARGIE'S TRAVEL
Encapsulation
MARGIE'S TRAVEL
M
9
Setter
void method
with parameter (based on the private variable type)
MARGIE'S TRAVEL
Getter
return method (based on the private variable type)
no parameter
M
10
ENCAPSULATION
#3: Implement Encapsulation in C++ Class files
#include <iostream> //Header File File //CPP File
using namespace std;
#include "Tester.h" #ifndef TESTER_H #include "Tester.h"
#define TESTER_H #include <iostream>
int main() #include <iostream> using namespace std;
{ using namespace std;
Tester X; void Tester::setString(string X){
X.setString("Hello World"); class Tester A = X;
cout<<X.getString(); { }
return 0; public:
MARGIE'S TRAVEL
private:
string A;
};
#endif // TESTER_H
M
11
Static Variables
is a variable created for the entire
class and is shared by all the
objects of that class no matter
MARGIE'S TRAVEL
Normal Static
Variable Variable
CLASS CLASS
MARGIE'S TRAVEL
M
13
Normal Static
Variable Variable
MARGIE'S TRAVEL
M
14
Static Function
are function belongs to the class rather
than the object of a class.
A static function can be invoked
without the need for creating an
MARGIE'S TRAVEL
instance of a class.
It can access only other static function
(methods or variables) in the same
M class.
15
STATIC MEMBERS
#4: Implement Static Members in C++ Class files
#include <iostream> //Header File File //CPP File
using namespace std;
#include "Testing.h" #ifndef TESTING_H #include "Testing.h"
#define TESTING_H #include <iostream>
int main() #include <iostream> using namespace std;
{ using namespace std;
Testing objA; int Testing::A;
objA.addB(); class Testing
objA.addB(); { void Testing::addB(){
cout<<objA.getB()<<endl; public: A=A+1;
MARGIE'S TRAVEL
M
16
Inheritance
is the process by which one class
acquire the attributes and behavior
of another class.
MARGIE'S TRAVEL
Attributes Behaviors
Black Hair
Choleric
PARENT Blue Eyes
Tall Built
Introvert
Talent: Singing
Brown Skin
MARGIE'S TRAVEL
Attributes Behaviors
Black Hair
Choleric
Blue Eyes
CHILD Tall Built
Introvert
Talent: Singing
Brown Skin
M
18
MARGIE'S TRAVEL
INHERITANCE
#5: Implement Inheritance in C++ Class files
#include "Base.h" //Header File File //Header File File
#include "SubClass.h"
#include <iostream> #ifndef BASE_H #ifndef SUBCLASS_H
using namespace std; #define BASE_H #define SUBCLASS_H
#include <iostream> #include <iostream>
int main() using namespace std; using namespace std;
{ #include "Base.h"
SubClass X; class Base
X.Display(); { class SubClass:public Base
return 0; public: {
MARGIE'S TRAVEL
void Base::Display(){
cout<<"This is Base";
M }
20
Abstraction
is the process of hiding certain details
and showing only essential to the user
hiding the function implementation
MARGIE'S TRAVEL
M
21
ABSTRACTION
#6: Implement Abstraction in C++ Class files
#include "TestClass.h" //Header File File //Header File File
#include "Childe.h"
#include <iostream> #ifndef TESTCLASS_H #ifndef CHILDE_H
using namespace std; #define TESTCLASS_H #define CHILDE_H
#include <iostream> #include <iostream>
int main() using namespace std; using namespace std;
{ #include "TestClass.h"
Childe A; class TestClass
A.Display(); { class Childe:public TestClass
return 0; public: {
MARGIE'S TRAVEL
void Childe::Display(){
cout<<"Implemented";
M }