0% found this document useful (0 votes)
7 views22 pages

DSA6 OOP Concepts

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views22 pages

DSA6 OOP Concepts

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

1

MARGIE'S TRAVEL

Data Structures
and Algorithms
T- C P E T 1 2 1

M
2

Colossians 3:23 (NIV)


Whatever you do, work at it with
MARGIE'S TRAVEL

all your heart, as working for the


Lord, not for human masters.
M
3

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

 automatically called when an object of


the class is created.

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

 the variable is not accessible to the


outside world, and only those functions
which are wrapped in the class can
M access it.
8

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

} void setString(string); string Tester::getString(){


string getString(); return A;
protected: }

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

how many objects are created.


 Only one copy of that variable is
M
created.
12

Normal Static
Variable Variable

CLASS CLASS
MARGIE'S TRAVEL

object object object object

object object object object

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

void addB(); B++;


Testing objB; int getB(); }
objB.addB(); static int getA();
objB.addB(); protected: int Testing::getB(){
cout<<objB.getB()<<endl; return B;
cout<<Testing::getA(); private: }
} static int A;
int B = 0; int Testing::getA(){
}; return A;
}
#endif // TESTING_H

M
16

Inheritance
 is the process by which one class
acquire the attributes and behavior
of another class.
MARGIE'S TRAVEL

 This also provides an opportunity


to reuse the code functionality and
M
fast implementation time.
17

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

Base Class Derived Class


Superclass Subclass
Parent Class Child Class
M
19

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 Display(); public:


}; };

#endif // BASE_H #endif // SUBCLASS_H

//CPP File //CPP File

#include "Base.h" #include "SubClass.h"


#include <iostream> #include <iostream>
using namespace std; using namespace std;

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

details from the user, only the


declaration will be provided.

M
21

 An abstract class is one that is not used to


create objects. An abstract class is designed
only to act as a base class (to be inherited by
other classes), a template of all derived class
 A pure virtual function is a function declared
in a base class that has no definition relative to
MARGIE'S TRAVEL

the base class. The compiler requires each


derived class to either define the function.

virtual void display()=0;


M
22

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

} virtual void Display()=0; public:


}; void Display();
};
#endif // TESTCLASS_H
#endif // CHILDE_H

//CPP File //CPP File

#include "TestClass.h" #include "Childe.h"


#include <iostream> #include <iostream>
using namespace std; using namespace std;

void Childe::Display(){
cout<<"Implemented";

M }

You might also like