SlideShare a Scribd company logo
OOP in C++ Ganesh Samarthyam
ganesh@codeops.tech
“There are two ways of
constructing a software
design: One way is to make
it so simple that there are
obviously no deficiencies,
and the other way is to make
it so complicated that there
are no obvious deficiencies.
The first method is far more
difficult.”
C. A. R. Hoare
Example of beautiful design
int arr[] = {1, 4, 9, 16, 25}; // some values
// find the first occurrence of 9 in the array
int * arr_pos = find(arr, arr + 4, 9);
std::cout<< “array pos = “<< arr_pos - arr << endl;
vector<int> int_vec;
for(int i = 1; i <= 5; i++)
int_vec.push_back(i*i);
vector<int>::iterator vec_pos = find (int_vec.begin(), int_vec.end(), 9);
std::cout<< “vector pos = “<< (vec_pos - int_vec.begin());
"The critical design tool for software development
is a mind well educated in design principles"
- Craig Larman
Robert C. Martin
Formulated many principles and described
many other important principles
Michael Feathers
Michael Feathers coined the acronym
SOLID in 2000s to remember first five of the
numerous principles by Robert C. Martin
SOLID principles
S
Single Responsibility
Principle
Every object should have a single responsibility and
that should be encapsulated by the class
O Open Closed Principle
Software should be open for extension, but closed for
modification
L
Liskov’s Substitution
Principle
Any subclass should always be usable instead of its
parent class
I
Interface Segregation
Principle
Many client specific interfaces are better than one
general purpose interface
D
Dependency Inversion
Principle
Abstractions should not depend upon details. Details
should depend upon abstractions
3 principles behind patterns
Design'principles'
behind'pa0erns'
Program'to'an'
interface,'not'to'an'
implementa7on''
Favor'object'
composi7on''
over'inheritance'
Encapsulate'what'
varies'
Booch’s fundamental principles
Principles*
Abstrac/on*
Encapsula/on*
Modulariza/on*
Hierarchy*
How to apply principles in practice?
Design principles
Code
How to bridge
the gap?
Why care about refactoring?
As an evolving program is
continually changed, its
complexity, reflecting
deteriorating structure,
increases unless work is done
to maintain or reduce it
- Lehman's law of Increasing Complexity
What is refactoring?
Refactoring (noun): a change
made to the internal structure of
software to make it easier to
understand and cheaper to
modify without changing its
observable behavior
Refactor (verb): to restructure
software by applying a series
of refactorings without
changing its observable
behavior
What are smells?
“Smells'are'certain'structures'
in'the'code'that'suggest'
(some4mes'they'scream'for)'
the'possibility'of'refactoring.”''
Granularity of smells
Architectural+
+
Cyclic&dependencies&between&modules&
Monolithic&modules&&
Layering&viola9ons&(back&layer&call,&skip&layer&call,&ver9cal&layering,&
etc)&
Design+
+
God&class&
Refused&bequest&&
Cyclic&dependencies&between&classes&
Code+(implementa6on)++
+
Internal&duplica9on&(clones&within&a&class)&&
Large&method&&
Temporary&field&&
Procedural code
#include <string>
#include <iostream>
using namespace std;
enum Animal { rat, pig, frog };
string talk(enum Animal animal) {
switch(animal) {
case rat: return "squeak";
case pig: return "oink";
case frog: return "ribbit";
default: return "unrecognized case value";
}
}
int main() {
enum Animal animal = pig;
cout << talk(animal) << endl;
}
Object oriented code
#include <string>
#include <iostream>
using namespace std;
class Animal {
public:
virtual string talk() = 0;
};
class Rat : public Animal {
public:
string talk() override { return "squeak"; }
};
class Pig : public Animal {
public:
string talk() override { return "oink"; }
};
class Frog : public Animal {
public:
string talk() override { return "ribbit"; }
};
int main() {
Pig pig;
Animal &animal = pig;
cout << animal.talk() << endl;
}
Functional code
#include <iostream>
using namespace std;
int main() {
auto rat = []() { return "squeak"; };
auto pig = []() { return "oink"; };
auto frog = []() { return "ribbit"; };
auto animaltalk = pig;
cout << animaltalk() << endl;
}
Generic code
#include <string>
#include <iostream>
using namespace std;
enum Animal {
rat, pig, frog
};
typedef const char * cstring;
template <enum Animal animal>
struct talk {
static constexpr cstring value = "undefined";
};
template <>
struct talk<rat> {
static constexpr cstring value = "squeak";
};
template <>
struct talk<pig> {
static constexpr cstring value = "oink";
};
template <>
struct talk<frog> {
static constexpr cstring value = "ribbit";
};
int main() {
cout << talk<pig>::value << endl;
}
Object Oriented Design
- Essentials
Inheritance vs. composition
❖ A common base class means common behaviour to its
derived types
❖ Public inheritance means “is-a” relationship
❖ Private inheritance means “is-implemented-in-terms-of”
❖ Composition means “has-a” or “is-implemented-in-
terms-of”
Virtual or non-virtual methods?
❖ Use pure virtual when you want to provide an interface
to the derived types
❖ Use virtual functions when you want to provide an
interface to its derived types with default behaviour
❖ Use non-virtual functions when you want to provide
the interface to its derived types with fixed behaviour
Templates vs. inheritance
❖ Does the type of the object affect the behaviour of the
functions in the class?
❖ If the answer is YES, then use Inheritance to model
❖ If the answer is NO, then use Generics/templates to
model
Object Oriented Design
- Best Practices
– Peter Wegner (https://en.wikipedia.org/wiki/Peter_Wegner)
“Object Oriented = Objects + Classes + Inheritance!”
Design Practices: Even in C!
Consider	the	following	methods	from	the	C	library:	
void bcopy(const void *src, void *dst, size_t n);
void *memcpy(void *dst, const void *src, size_t n);
What	is	the	problem	in	the	interfaces	of	these	methods?	Does	it	follow	the	principles	of	good	API	design?
Design Practices: Even in C!
realloc	-	“does	too	many	things!”	
#include <cstdlib>
using namespace std;
// demonstrates how malloc acts as a "complete memory manager!"
int main()
{
int* ip = (int*) realloc(NULL, sizeof(int) * 100);
// allocate 100 4-byte ints - equivalent to malloc(sizeof(int) * 100)
ip = (int*) realloc(ip, sizeof(int) * 200); // expand the memory area twice
ip = (int*) realloc(ip, sizeof(int) * 50); // shrink to half the memory area
ip = (int*) realloc(ip, 0); // equivalent to free(ip)
}
Design Practices: Even in C!
strtok	-	“stateful	methods!”	(not	reentrant)
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Mon Jul 17 17:56:41 IST 2017";
char *token = strtok(str, ". -,;:");
do {
printf("%s n", token);
}
while(token = strtok(0, ". -,;:"));
}
Intuitive class design?
class	MyContainer {
private:	
int m_size;
public:	
void	setLength(int size)	{	m_size=	size;	}
void	setSize(int size)	{	m_size=	size;	}
int getLength()	{	return	m_size;	}
int getSize()	{	return	m_size;	}	
//	other	members	
}
Intuitive class design?
MyContainer * container = new Container;
container.setSize(0);
container.setLength(20);
cout << “Container's size is ” << container.getSize() <<endl;
cout << “Its length is “ container.getLength() <<endl;
// output:
// Container's size is 20
// Its length is 20
Calling virtual methods from a ctor
struct base {
base() {
vfun();
}
virtual void vfun() {
cout << “Inside base::vfunn”;
}
};
struct deri : base {
virtual void vfun() {
cout << “Inside deri::vfunn”;
}
};
int main(){
deri d;
}
// prints:
// Inside base::vfun
Calling virtual methods from a ctor
struct base {
base() {
base * bptr = this;
bptr->bar();
}
virtual void bar() =0;
};
struct deri: base {
void bar(){ }
};
int main() {
deri d;
}
// g++ output:
// pure virtual method called
// ABORT instruction (core dumped)
Does it happen in real-world?
• Supporting multimedia
software for my Blackberry
crashed with this design error!
• Classic OO design problem of
“polymorphic call in
constructors”
• Constructors do not support fully
as the derived objects are not
constructed yet when base class
constructor executes
Best Practice
“Avoid calling virtual functions from constructors”
Temporary objects & NRV
class String {
public:
String(const char *str) : cstr(str) {}
String(const String& arg) {
std::cout<< “String cctor n”;
this->cstr = arg.cstr;
}
private:
const char * cstr;
}
String function(){
return String("Hello");
}
int main(){
String s = function();
}
// without NRV optimization on, this prints:
// String cctor
// with NRV optimization on, this prints:
// String cctor
// String cctor
Intuitive overloading?
Colour (int red, int green, int blue);
Colour (float hue, float saturation, float brightness);
Intuitive overloading?
static Colour* createRGBColour(int red, int blue, int green);
static Colour* createHSBColour(float hue, float saturation, float brightness);
Intuitive overloading?
void log(double val) {
// prints the natural logarithm value of val
}
void log(String str) {
// logs the string str into the log file
}
Intuitive overloading?
void log(double val) {
// prints the natural logarithm value of val
}
void log(String str) {
// logs the string str into the log file
}
void logarithm(double val);
void logger(String str);
// or
void log(double val);
void logger(String str);
Best Practice
“Avoid confusing overloads”
Preventing inheritance
// C++ Example
class NoInherit {
private:
NoInherit() {
// code for constructor
}
public:
static NoInherit* createInstance() {
return new NoInherit();
}
};
// Creation of the object
NoInherit* nip = NoInherit::createInstance();
Beware of versioning problems
class Base { // provided by a third party tool
public:
virtual void vfoo(){ // introduced in version 2.0
cout<< ”vfoo in Base – introduced in a new version”;
}
// other members
};
class Derived : public Base { // some client code
public:
void vfoo(){ // was available in version 1.0
cout<< ”vfoo in Deri – now becomes overridden”;
}
// other members
};
Follow “safe” overriding
#include <iostream>
#include <memory>
using namespace std;
class Base {
public:
virtual void call(int val = 10) {
cout << "The default value is: " << val << endl;
}
};
class Derived : public Base {
public:
virtual void call(int val = 20) {
cout << "The default value is: " << val << endl;
}
};
int main() {
unique_ptr<Base> b = make_unique<Derived>();
b->call();
}
// prints
// The default value is: 10
Follow “safe” overriding
#include <iostream>
#include <memory>
using namespace std;
class Base {
public:
void call() {
cout << "In Base::call" << endl;
}
};
class Derived : public Base {
public:
virtual void call() {
cout << "In Derived::call" << endl;
}
};
int main() {
unique_ptr<Base> b = make_unique<Derived>();
b->call();
}
// prints
// In Base::call
Follow “safe” overriding
#include <iostream>
#include <memory>
using namespace std;
class Base {
public:
virtual void call() {
cout << "in Base::call" << endl;
}
};
class Derived : public Base {
public:
virtual void call() const {
cout << "in Derived::call" << endl;
}
};
int main() {
unique_ptr<Base> b = make_unique<Derived>();
b->call();
}
// prints
// In Base::call
Best Practice
“Use ‘override’ keyword for safe overriding”
Overloading and overriding
#include <iostream>
#include <memory>
using namespace std;
class Base {
public:
virtual void call(char ch) {
cout << "In Base::call(char)" << endl;
}
virtual void call(int i) {
cout << "In Base::call(int)" << endl;
}
};
class Derived : public Base {
public:
virtual void call(char ch) {
cout << "In Derived::call(char)" << endl;
}
};
int main() {
unique_ptr<Base> b = make_unique<Derived>();
b->call(10);
}
Selectively introduce types
// in mymath.h
namespace math {
class scalar { /* … */ }
class vector { /* … */ }
// other members
}
// in use.cpp
// for using std::vector:
#include <vector>
using namespace std;
// for using the scalar class in your math namespace:
#include “mymath.h”
using namespace math;
int main() {
vector<scalar *> v;
// compiler error: vector is ambiguous
// does vector refer to std::vector or math::vector?
}
Selectively expose types to clients
namespace {
int someMem;
void somefunction();
};
// is a better way to limit names to file scope
// than the following:
static int someMem;
static void somefunction();
Hiding?
int x, y; // global variables x and y
class Point {
public:
int x, y; // class members x and y
Point(int x, int y); // function arguments x and y
};
// Point constructor for setting values of x and y data members
// C++
Point::Point(int x, int y) {
x = x;
y = y;
}
Beware of hiding
void foo { // outer block
int x, y;
{ // inner block
int x = 10, y = 20;
// hides the outer x and y
}
}
Long parameter lists
Are arrays polymorphic?
class base {
int basemem;
public:
base() : basemem(10) {}
int getint() {
return basemem;
}
// other members
};
class derived : public base {
float derivedmem;
public:
derived() : base(), derivedmem(20.0f) {}
// other members
};
void print(base *bPtr, int size) {
for(int i = 0; i < size; i++, bPtr++)
cout<< bPtr->getint() << endl;
}
int main() {
base b[5];
// prints five 10's correctly
print(b, 5);
derived d[5];
// does not print five 10's correctly
print(d, 5);
}
Books to read
Effective Object Oriented Design in Cpp
Effective Object Oriented Design in Cpp
Effective Object Oriented Design in Cpp
Effective Object Oriented Design in Cpp
Effective Object Oriented Design in Cpp
“Applying design principles is the key to creating
high-quality software!”
Architectural principles:
Axis, symmetry, rhythm, datum, hierarchy, transformation
Effective Object Oriented Design in Cpp
Image/video credits
❖ http://en.wikipedia.org/wiki/Fear_of_missing_out
❖ http://lesliejanemoran.blogspot.in/2010_05_01_archive.html
❖ http://javra.eu/wp-content/uploads/2013/07/angry_laptop2.jpg
❖ https://www.youtube.com/watch?v=5R8XHrfJkeg
❖ http://womenworld.org/image/052013/31/113745161.jpg
❖ http://www.fantom-xp.com/wallpapers/33/I'm_not_sure.jpg
❖ https://www.flickr.com/photos/31457017@N00/453784086
❖ https://www.gradtouch.com/uploads/images/question3.jpg
❖ http://gurujohn.files.wordpress.com/2008/06/bookcover0001.jpg
❖ http://upload.wikimedia.org/wikipedia/commons/d/d5/Martin_Fowler_-_Swipe_Conference_2012.jpg
❖ http://www.codeproject.com/KB/architecture/csdespat_2/dpcs_br.gif
❖ http://upload.wikimedia.org/wikipedia/commons/thumb/2/28/Bertrand_Meyer_IMG_2481.jpg/440px-
Bertrand_Meyer_IMG_2481.jpg
❖ http://takeji-soft.up.n.seesaa.net/takeji-soft/image/GOF-OOPLSA-94-Color-75.jpg?d=a0
❖ https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/OOP_ObjC/Art/watchcalls_35.gif
❖ http://www.pluspack.com/files/billeder/Newsletter/25/takeaway_bag.png
❖ http://cdn1.tnwcdn.com/wp-content/blogs.dir/1/files/2013/03/design.jpg
❖ http://img01.deviantart.net/d8ab/i/2016/092/c/2/may_the_force_be_with_you___yoda_flag_by_osflag-d9xe904.jpg
ganesh@codeops.tech @GSamarthyam
www.codeops.tech slideshare.net/sgganesh
+91 98801 64463 bit.ly/sgganesh

More Related Content

What's hot (20)

PPTX
Summary of C++17 features
Bartlomiej Filipek
 
PDF
Building DSLs with Xtext - Eclipse Modeling Day 2009
Heiko Behrens
 
PDF
Modern C++ Explained: Move Semantics (Feb 2018)
Olve Maudal
 
PPT
Project Lambda - Closures after all?
Andreas Enbohm
 
PPTX
FParsec Hands On - F#unctional Londoners 2014
Phillip Trelford
 
PDF
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Tudor Dragan
 
PDF
Kotlin Developer Starter in Android projects
Bartosz Kosarzycki
 
PPTX
Code is not text! How graph technologies can help us to understand our code b...
Andreas Dewes
 
PPTX
The Style of C++ 11
Sasha Goldshtein
 
PDF
Xtext Webinar
Heiko Behrens
 
PDF
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
Francesco Casalegno
 
PPTX
Learning from other's mistakes: Data-driven code analysis
Andreas Dewes
 
PPTX
C++11: Feel the New Language
mspline
 
PDF
C++11 & C++14
CyberPlusIndia
 
PDF
CS4200 2019 | Lecture 3 | Parsing
Eelco Visser
 
PPTX
Kotlin as a Better Java
Garth Gilmour
 
PDF
Modern C++
Michael Clark
 
PDF
Writing Parsers and Compilers with PLY
David Beazley (Dabeaz LLC)
 
PDF
The Rust Programming Language: an Overview
Roberto Casadei
 
Summary of C++17 features
Bartlomiej Filipek
 
Building DSLs with Xtext - Eclipse Modeling Day 2009
Heiko Behrens
 
Modern C++ Explained: Move Semantics (Feb 2018)
Olve Maudal
 
Project Lambda - Closures after all?
Andreas Enbohm
 
FParsec Hands On - F#unctional Londoners 2014
Phillip Trelford
 
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Tudor Dragan
 
Kotlin Developer Starter in Android projects
Bartosz Kosarzycki
 
Code is not text! How graph technologies can help us to understand our code b...
Andreas Dewes
 
The Style of C++ 11
Sasha Goldshtein
 
Xtext Webinar
Heiko Behrens
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
Francesco Casalegno
 
Learning from other's mistakes: Data-driven code analysis
Andreas Dewes
 
C++11: Feel the New Language
mspline
 
C++11 & C++14
CyberPlusIndia
 
CS4200 2019 | Lecture 3 | Parsing
Eelco Visser
 
Kotlin as a Better Java
Garth Gilmour
 
Modern C++
Michael Clark
 
Writing Parsers and Compilers with PLY
David Beazley (Dabeaz LLC)
 
The Rust Programming Language: an Overview
Roberto Casadei
 

Similar to Effective Object Oriented Design in Cpp (20)

PDF
OO Design and Design Patterns in C++
Ganesh Samarthyam
 
PDF
L10
lksoo
 
PDF
CS225_Prelecture_Notes 2nd
Edward Chen
 
PPT
lecture02-cpp.ppt
DevliNeeraj
 
PPT
lecture02-cpp.ppt
ssuser0c24d5
 
PPT
lecture02-cpp.ppt
nilesh405711
 
PPT
lecture02-cpp.ppt
YashpalYadav46
 
PPT
c++ ppt.ppt
FarazKhan89093
 
PDF
How to make a large C++-code base manageable
corehard_by
 
PPTX
Introduction to c ++ part -1
baabtra.com - No. 1 supplier of quality freshers
 
PPT
lecture02-cpp.ppt
MZGINBarwary
 
DOCX
C++ & Design Patterns Quicky
Nikunj Parekh
 
DOCX
C++ & Design Patterns - primera parte
Nikunj Parekh
 
PDF
Mastering Modern C++: C++11, C++14, C++17, C++20, C++23
Massimo Talia
 
PPTX
lecture NOTES ON OOPS C++ ON CLASS AND OBJECTS
NagarathnaRajur2
 
PPTX
OOP_in_CPP_Animesh_Animated_Diagram.pptx
animesh713331
 
PDF
The Evolution of Good Code
Arjan van Leeuwen
 
PDF
Classes
Swarup Boro
 
PPTX
C++ & Data Structure - Unit - first.pptx
KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY
 
OO Design and Design Patterns in C++
Ganesh Samarthyam
 
L10
lksoo
 
CS225_Prelecture_Notes 2nd
Edward Chen
 
lecture02-cpp.ppt
DevliNeeraj
 
lecture02-cpp.ppt
ssuser0c24d5
 
lecture02-cpp.ppt
nilesh405711
 
lecture02-cpp.ppt
YashpalYadav46
 
c++ ppt.ppt
FarazKhan89093
 
How to make a large C++-code base manageable
corehard_by
 
lecture02-cpp.ppt
MZGINBarwary
 
C++ & Design Patterns Quicky
Nikunj Parekh
 
C++ & Design Patterns - primera parte
Nikunj Parekh
 
Mastering Modern C++: C++11, C++14, C++17, C++20, C++23
Massimo Talia
 
lecture NOTES ON OOPS C++ ON CLASS AND OBJECTS
NagarathnaRajur2
 
OOP_in_CPP_Animesh_Animated_Diagram.pptx
animesh713331
 
The Evolution of Good Code
Arjan van Leeuwen
 
Classes
Swarup Boro
 
C++ & Data Structure - Unit - first.pptx
KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY
 
Ad

More from CodeOps Technologies LLP (20)

PDF
AWS Serverless Event-driven Architecture - in lastminute.com meetup
CodeOps Technologies LLP
 
PPTX
Understanding azure batch service
CodeOps Technologies LLP
 
PDF
DEVOPS AND MACHINE LEARNING
CodeOps Technologies LLP
 
PDF
SERVERLESS MIDDLEWARE IN AZURE FUNCTIONS
CodeOps Technologies LLP
 
PPT
BUILDING SERVERLESS SOLUTIONS WITH AZURE FUNCTIONS
CodeOps Technologies LLP
 
PPTX
APPLYING DEVOPS STRATEGIES ON SCALE USING AZURE DEVOPS SERVICES
CodeOps Technologies LLP
 
PPTX
BUILD, TEST & DEPLOY .NET CORE APPS IN AZURE DEVOPS
CodeOps Technologies LLP
 
PPTX
CREATE RELIABLE AND LOW-CODE APPLICATION IN SERVERLESS MANNER
CodeOps Technologies LLP
 
PPTX
CREATING REAL TIME DASHBOARD WITH BLAZOR, AZURE FUNCTION COSMOS DB AN AZURE S...
CodeOps Technologies LLP
 
PPTX
WRITE SCALABLE COMMUNICATION APPLICATION WITH POWER OF SERVERLESS
CodeOps Technologies LLP
 
PPTX
Training And Serving ML Model Using Kubeflow by Jayesh Sharma
CodeOps Technologies LLP
 
PPTX
Deploy Microservices To Kubernetes Without Secrets by Reenu Saluja
CodeOps Technologies LLP
 
PDF
Leverage Azure Tech stack for any Kubernetes cluster via Azure Arc by Saiyam ...
CodeOps Technologies LLP
 
PDF
YAML Tips For Kubernetes by Neependra Khare
CodeOps Technologies LLP
 
PDF
Must Know Azure Kubernetes Best Practices And Features For Better Resiliency ...
CodeOps Technologies LLP
 
PPTX
Monitor Azure Kubernetes Cluster With Prometheus by Mamta Jha
CodeOps Technologies LLP
 
PDF
Jet brains space intro presentation
CodeOps Technologies LLP
 
PDF
Functional Programming in Java 8 - Lambdas and Streams
CodeOps Technologies LLP
 
PPTX
Distributed Tracing: New DevOps Foundation
CodeOps Technologies LLP
 
PDF
"Distributed Tracing: New DevOps Foundation" by Jayesh Ahire
CodeOps Technologies LLP
 
AWS Serverless Event-driven Architecture - in lastminute.com meetup
CodeOps Technologies LLP
 
Understanding azure batch service
CodeOps Technologies LLP
 
DEVOPS AND MACHINE LEARNING
CodeOps Technologies LLP
 
SERVERLESS MIDDLEWARE IN AZURE FUNCTIONS
CodeOps Technologies LLP
 
BUILDING SERVERLESS SOLUTIONS WITH AZURE FUNCTIONS
CodeOps Technologies LLP
 
APPLYING DEVOPS STRATEGIES ON SCALE USING AZURE DEVOPS SERVICES
CodeOps Technologies LLP
 
BUILD, TEST & DEPLOY .NET CORE APPS IN AZURE DEVOPS
CodeOps Technologies LLP
 
CREATE RELIABLE AND LOW-CODE APPLICATION IN SERVERLESS MANNER
CodeOps Technologies LLP
 
CREATING REAL TIME DASHBOARD WITH BLAZOR, AZURE FUNCTION COSMOS DB AN AZURE S...
CodeOps Technologies LLP
 
WRITE SCALABLE COMMUNICATION APPLICATION WITH POWER OF SERVERLESS
CodeOps Technologies LLP
 
Training And Serving ML Model Using Kubeflow by Jayesh Sharma
CodeOps Technologies LLP
 
Deploy Microservices To Kubernetes Without Secrets by Reenu Saluja
CodeOps Technologies LLP
 
Leverage Azure Tech stack for any Kubernetes cluster via Azure Arc by Saiyam ...
CodeOps Technologies LLP
 
YAML Tips For Kubernetes by Neependra Khare
CodeOps Technologies LLP
 
Must Know Azure Kubernetes Best Practices And Features For Better Resiliency ...
CodeOps Technologies LLP
 
Monitor Azure Kubernetes Cluster With Prometheus by Mamta Jha
CodeOps Technologies LLP
 
Jet brains space intro presentation
CodeOps Technologies LLP
 
Functional Programming in Java 8 - Lambdas and Streams
CodeOps Technologies LLP
 
Distributed Tracing: New DevOps Foundation
CodeOps Technologies LLP
 
"Distributed Tracing: New DevOps Foundation" by Jayesh Ahire
CodeOps Technologies LLP
 
Ad

Recently uploaded (20)

PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
DOCX
Import Data Form Excel to Tally Services
Tally xperts
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Import Data Form Excel to Tally Services
Tally xperts
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
Human Resources Information System (HRIS)
Amity University, Patna
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 

Effective Object Oriented Design in Cpp

  • 1. OOP in C++ Ganesh Samarthyam [email protected]
  • 2. “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” C. A. R. Hoare
  • 3. Example of beautiful design int arr[] = {1, 4, 9, 16, 25}; // some values // find the first occurrence of 9 in the array int * arr_pos = find(arr, arr + 4, 9); std::cout<< “array pos = “<< arr_pos - arr << endl; vector<int> int_vec; for(int i = 1; i <= 5; i++) int_vec.push_back(i*i); vector<int>::iterator vec_pos = find (int_vec.begin(), int_vec.end(), 9); std::cout<< “vector pos = “<< (vec_pos - int_vec.begin());
  • 4. "The critical design tool for software development is a mind well educated in design principles" - Craig Larman
  • 5. Robert C. Martin Formulated many principles and described many other important principles
  • 6. Michael Feathers Michael Feathers coined the acronym SOLID in 2000s to remember first five of the numerous principles by Robert C. Martin
  • 7. SOLID principles S Single Responsibility Principle Every object should have a single responsibility and that should be encapsulated by the class O Open Closed Principle Software should be open for extension, but closed for modification L Liskov’s Substitution Principle Any subclass should always be usable instead of its parent class I Interface Segregation Principle Many client specific interfaces are better than one general purpose interface D Dependency Inversion Principle Abstractions should not depend upon details. Details should depend upon abstractions
  • 8. 3 principles behind patterns Design'principles' behind'pa0erns' Program'to'an' interface,'not'to'an' implementa7on'' Favor'object' composi7on'' over'inheritance' Encapsulate'what' varies'
  • 10. How to apply principles in practice? Design principles Code How to bridge the gap?
  • 11. Why care about refactoring? As an evolving program is continually changed, its complexity, reflecting deteriorating structure, increases unless work is done to maintain or reduce it - Lehman's law of Increasing Complexity
  • 12. What is refactoring? Refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior Refactor (verb): to restructure software by applying a series of refactorings without changing its observable behavior
  • 15. Procedural code #include <string> #include <iostream> using namespace std; enum Animal { rat, pig, frog }; string talk(enum Animal animal) { switch(animal) { case rat: return "squeak"; case pig: return "oink"; case frog: return "ribbit"; default: return "unrecognized case value"; } } int main() { enum Animal animal = pig; cout << talk(animal) << endl; }
  • 16. Object oriented code #include <string> #include <iostream> using namespace std; class Animal { public: virtual string talk() = 0; }; class Rat : public Animal { public: string talk() override { return "squeak"; } }; class Pig : public Animal { public: string talk() override { return "oink"; } }; class Frog : public Animal { public: string talk() override { return "ribbit"; } }; int main() { Pig pig; Animal &animal = pig; cout << animal.talk() << endl; }
  • 17. Functional code #include <iostream> using namespace std; int main() { auto rat = []() { return "squeak"; }; auto pig = []() { return "oink"; }; auto frog = []() { return "ribbit"; }; auto animaltalk = pig; cout << animaltalk() << endl; }
  • 18. Generic code #include <string> #include <iostream> using namespace std; enum Animal { rat, pig, frog }; typedef const char * cstring; template <enum Animal animal> struct talk { static constexpr cstring value = "undefined"; }; template <> struct talk<rat> { static constexpr cstring value = "squeak"; }; template <> struct talk<pig> { static constexpr cstring value = "oink"; }; template <> struct talk<frog> { static constexpr cstring value = "ribbit"; }; int main() { cout << talk<pig>::value << endl; }
  • 20. Inheritance vs. composition ❖ A common base class means common behaviour to its derived types ❖ Public inheritance means “is-a” relationship ❖ Private inheritance means “is-implemented-in-terms-of” ❖ Composition means “has-a” or “is-implemented-in- terms-of”
  • 21. Virtual or non-virtual methods? ❖ Use pure virtual when you want to provide an interface to the derived types ❖ Use virtual functions when you want to provide an interface to its derived types with default behaviour ❖ Use non-virtual functions when you want to provide the interface to its derived types with fixed behaviour
  • 22. Templates vs. inheritance ❖ Does the type of the object affect the behaviour of the functions in the class? ❖ If the answer is YES, then use Inheritance to model ❖ If the answer is NO, then use Generics/templates to model
  • 23. Object Oriented Design - Best Practices
  • 24. – Peter Wegner (https://en.wikipedia.org/wiki/Peter_Wegner) “Object Oriented = Objects + Classes + Inheritance!”
  • 25. Design Practices: Even in C! Consider the following methods from the C library: void bcopy(const void *src, void *dst, size_t n); void *memcpy(void *dst, const void *src, size_t n); What is the problem in the interfaces of these methods? Does it follow the principles of good API design?
  • 26. Design Practices: Even in C! realloc - “does too many things!” #include <cstdlib> using namespace std; // demonstrates how malloc acts as a "complete memory manager!" int main() { int* ip = (int*) realloc(NULL, sizeof(int) * 100); // allocate 100 4-byte ints - equivalent to malloc(sizeof(int) * 100) ip = (int*) realloc(ip, sizeof(int) * 200); // expand the memory area twice ip = (int*) realloc(ip, sizeof(int) * 50); // shrink to half the memory area ip = (int*) realloc(ip, 0); // equivalent to free(ip) }
  • 27. Design Practices: Even in C! strtok - “stateful methods!” (not reentrant) #include <stdio.h> #include <string.h> int main() { char str[] = "Mon Jul 17 17:56:41 IST 2017"; char *token = strtok(str, ". -,;:"); do { printf("%s n", token); } while(token = strtok(0, ". -,;:")); }
  • 28. Intuitive class design? class MyContainer { private: int m_size; public: void setLength(int size) { m_size= size; } void setSize(int size) { m_size= size; } int getLength() { return m_size; } int getSize() { return m_size; } // other members }
  • 29. Intuitive class design? MyContainer * container = new Container; container.setSize(0); container.setLength(20); cout << “Container's size is ” << container.getSize() <<endl; cout << “Its length is “ container.getLength() <<endl; // output: // Container's size is 20 // Its length is 20
  • 30. Calling virtual methods from a ctor struct base { base() { vfun(); } virtual void vfun() { cout << “Inside base::vfunn”; } }; struct deri : base { virtual void vfun() { cout << “Inside deri::vfunn”; } }; int main(){ deri d; } // prints: // Inside base::vfun
  • 31. Calling virtual methods from a ctor struct base { base() { base * bptr = this; bptr->bar(); } virtual void bar() =0; }; struct deri: base { void bar(){ } }; int main() { deri d; } // g++ output: // pure virtual method called // ABORT instruction (core dumped)
  • 32. Does it happen in real-world? • Supporting multimedia software for my Blackberry crashed with this design error! • Classic OO design problem of “polymorphic call in constructors” • Constructors do not support fully as the derived objects are not constructed yet when base class constructor executes
  • 33. Best Practice “Avoid calling virtual functions from constructors”
  • 34. Temporary objects & NRV class String { public: String(const char *str) : cstr(str) {} String(const String& arg) { std::cout<< “String cctor n”; this->cstr = arg.cstr; } private: const char * cstr; } String function(){ return String("Hello"); } int main(){ String s = function(); } // without NRV optimization on, this prints: // String cctor // with NRV optimization on, this prints: // String cctor // String cctor
  • 35. Intuitive overloading? Colour (int red, int green, int blue); Colour (float hue, float saturation, float brightness);
  • 36. Intuitive overloading? static Colour* createRGBColour(int red, int blue, int green); static Colour* createHSBColour(float hue, float saturation, float brightness);
  • 37. Intuitive overloading? void log(double val) { // prints the natural logarithm value of val } void log(String str) { // logs the string str into the log file }
  • 38. Intuitive overloading? void log(double val) { // prints the natural logarithm value of val } void log(String str) { // logs the string str into the log file } void logarithm(double val); void logger(String str); // or void log(double val); void logger(String str);
  • 40. Preventing inheritance // C++ Example class NoInherit { private: NoInherit() { // code for constructor } public: static NoInherit* createInstance() { return new NoInherit(); } }; // Creation of the object NoInherit* nip = NoInherit::createInstance();
  • 41. Beware of versioning problems class Base { // provided by a third party tool public: virtual void vfoo(){ // introduced in version 2.0 cout<< ”vfoo in Base – introduced in a new version”; } // other members }; class Derived : public Base { // some client code public: void vfoo(){ // was available in version 1.0 cout<< ”vfoo in Deri – now becomes overridden”; } // other members };
  • 42. Follow “safe” overriding #include <iostream> #include <memory> using namespace std; class Base { public: virtual void call(int val = 10) { cout << "The default value is: " << val << endl; } }; class Derived : public Base { public: virtual void call(int val = 20) { cout << "The default value is: " << val << endl; } }; int main() { unique_ptr<Base> b = make_unique<Derived>(); b->call(); } // prints // The default value is: 10
  • 43. Follow “safe” overriding #include <iostream> #include <memory> using namespace std; class Base { public: void call() { cout << "In Base::call" << endl; } }; class Derived : public Base { public: virtual void call() { cout << "In Derived::call" << endl; } }; int main() { unique_ptr<Base> b = make_unique<Derived>(); b->call(); } // prints // In Base::call
  • 44. Follow “safe” overriding #include <iostream> #include <memory> using namespace std; class Base { public: virtual void call() { cout << "in Base::call" << endl; } }; class Derived : public Base { public: virtual void call() const { cout << "in Derived::call" << endl; } }; int main() { unique_ptr<Base> b = make_unique<Derived>(); b->call(); } // prints // In Base::call
  • 45. Best Practice “Use ‘override’ keyword for safe overriding”
  • 46. Overloading and overriding #include <iostream> #include <memory> using namespace std; class Base { public: virtual void call(char ch) { cout << "In Base::call(char)" << endl; } virtual void call(int i) { cout << "In Base::call(int)" << endl; } }; class Derived : public Base { public: virtual void call(char ch) { cout << "In Derived::call(char)" << endl; } }; int main() { unique_ptr<Base> b = make_unique<Derived>(); b->call(10); }
  • 47. Selectively introduce types // in mymath.h namespace math { class scalar { /* … */ } class vector { /* … */ } // other members } // in use.cpp // for using std::vector: #include <vector> using namespace std; // for using the scalar class in your math namespace: #include “mymath.h” using namespace math; int main() { vector<scalar *> v; // compiler error: vector is ambiguous // does vector refer to std::vector or math::vector? }
  • 48. Selectively expose types to clients namespace { int someMem; void somefunction(); }; // is a better way to limit names to file scope // than the following: static int someMem; static void somefunction();
  • 49. Hiding? int x, y; // global variables x and y class Point { public: int x, y; // class members x and y Point(int x, int y); // function arguments x and y }; // Point constructor for setting values of x and y data members // C++ Point::Point(int x, int y) { x = x; y = y; }
  • 50. Beware of hiding void foo { // outer block int x, y; { // inner block int x = 10, y = 20; // hides the outer x and y } }
  • 52. Are arrays polymorphic? class base { int basemem; public: base() : basemem(10) {} int getint() { return basemem; } // other members }; class derived : public base { float derivedmem; public: derived() : base(), derivedmem(20.0f) {} // other members }; void print(base *bPtr, int size) { for(int i = 0; i < size; i++, bPtr++) cout<< bPtr->getint() << endl; } int main() { base b[5]; // prints five 10's correctly print(b, 5); derived d[5]; // does not print five 10's correctly print(d, 5); }
  • 59. “Applying design principles is the key to creating high-quality software!” Architectural principles: Axis, symmetry, rhythm, datum, hierarchy, transformation
  • 61. Image/video credits ❖ http://en.wikipedia.org/wiki/Fear_of_missing_out ❖ http://lesliejanemoran.blogspot.in/2010_05_01_archive.html ❖ http://javra.eu/wp-content/uploads/2013/07/angry_laptop2.jpg ❖ https://www.youtube.com/watch?v=5R8XHrfJkeg ❖ http://womenworld.org/image/052013/31/113745161.jpg ❖ http://www.fantom-xp.com/wallpapers/33/I'm_not_sure.jpg ❖ https://www.flickr.com/photos/31457017@N00/453784086 ❖ https://www.gradtouch.com/uploads/images/question3.jpg ❖ http://gurujohn.files.wordpress.com/2008/06/bookcover0001.jpg ❖ http://upload.wikimedia.org/wikipedia/commons/d/d5/Martin_Fowler_-_Swipe_Conference_2012.jpg ❖ http://www.codeproject.com/KB/architecture/csdespat_2/dpcs_br.gif ❖ http://upload.wikimedia.org/wikipedia/commons/thumb/2/28/Bertrand_Meyer_IMG_2481.jpg/440px- Bertrand_Meyer_IMG_2481.jpg ❖ http://takeji-soft.up.n.seesaa.net/takeji-soft/image/GOF-OOPLSA-94-Color-75.jpg?d=a0 ❖ https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/OOP_ObjC/Art/watchcalls_35.gif ❖ http://www.pluspack.com/files/billeder/Newsletter/25/takeaway_bag.png ❖ http://cdn1.tnwcdn.com/wp-content/blogs.dir/1/files/2013/03/design.jpg ❖ http://img01.deviantart.net/d8ab/i/2016/092/c/2/may_the_force_be_with_you___yoda_flag_by_osflag-d9xe904.jpg