Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview...
Transcript of Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview...
![Page 1: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/1.jpg)
Object Oriented Programming using C++
![Page 2: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/2.jpg)
Overview
Problem Solving
Features of an OOL
Basic Syntax
Programming Paradigms
![Page 3: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/3.jpg)
Solving a Programming Problem
Analysis
Design
Coding
Management
Programming paradigms
Programming languages
![Page 4: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/4.jpg)
Concepts & Relationships
– A rectangle uses lines
– A circle is an ellipse
– A wheel is part of automobile
– A set creates its elements
![Page 5: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/5.jpg)
Some Features of OOP languages
An OOP language should support – Easy Representation of
Real-world objects
Their States and Abilities
– Interaction with objects of same type
– Relations with objects of other type
– Polymorphism and Overloading
Reusability of code
Convenient type definitions
![Page 6: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/6.jpg)
Basic Syntax
Same as C
Additional operators
Additional keywords
![Page 7: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/7.jpg)
Programming Paradigms
Procedural Programming (functions)
Modular Programming (namespaces)
Object Oriented Programming (classes)
Generic Programming (templates)
![Page 8: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/8.jpg)
Procedural Programming
A program is a list of instructions
Concentration is on what is to be done?
Problems created by global data
No access control !!
![Page 9: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/9.jpg)
Modular Programming
namespace
Example to learn the syntax : A toy math library.
– Matrix Module
Allocate
Transpose
– Vector Module
Allocate
Transpose
![Page 10: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/10.jpg)
Declaration of namespaces
// in file stdmatrix.h
namespace Matrix
{
int** allocate(int r,int c);
void print(int **matrix,int r,int c);
};
// in file stdvector.h
namespace Vector
{
int *allocate(int size);
void print(int *vector,int size);
};
![Page 11: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/11.jpg)
Definition of Matrix functions
int **Matrix::allocate(int r, int c)
{
int **mat;
mat = new int*[r];
for (int i = 0; i < r; i++)
mat[i] = new int[c];
return mat;
}
void Matrix::print(int **matrix,int r, int c)
{
for (int i = 0; i < r ; i++)
{
for (int j = 0; j < c ; j++)
printf("%d ",matrix[i][j]);
printf("\n");
}
printf("\n");
}
![Page 12: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/12.jpg)
Definition of Vector functions
int *Vector::allocate(int size)
{
int *vec = new int[size];
return vec;
}
void Vector::print(int *vector,int size)
{
for (int i = 0; i < size; i++)
printf("%d ",vector[i]);
printf("\n");
}
![Page 13: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/13.jpg)
How to use Matrix and Vector ?
// in the file main.c
#include <stdmatrix.h>
#include <stdvector.h>
using namespace Matrix;
using namespace Vector;
main()
{
int **m = Matrix::allocate(3,4);
int *v = Vector::allocate(3);
Matrix::print(m,3,4);
Vector::print(v,3);
}
![Page 14: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/14.jpg)
Adding functions to namespaces
How does user add the transpose function now ?
// in the file mymatrix.h
#include “stdmatrix.h”
namespace Matrix
{
int **transpose(int **matrix, int r, int c);
}
![Page 15: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/15.jpg)
Adding definitions
// In the file mymatrix.cpp
#include “mymatrix.h”
int **Matrix::transpose(int **matrix, int r, int c)
{
// Code for transposing and returning the
// matrix.
}
![Page 16: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/16.jpg)
Using the transpose()
// in the file main.c
#include <mymatrix.h>
#include <myvector.h>
using namespace Matrix;
using namespace Vector;
main()
{
int **m = Matrix::allocate(3,4);
// fill with some random data in the matrix.
int **tm = Matrix::transpose(m, 3, 4);
}
![Page 17: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/17.jpg)
Object Oriented Programming
Class
Object
Overloading (operators, functions)
Inheritance
![Page 18: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/18.jpg)
Class
class : Definition of the entity
– A tool for creating new types
Ex: Matrix and Vector
– Access Control
public, private, and protected
– Constructors, Destructors
– Member functions
![Page 19: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/19.jpg)
The Matrix Class Definition
class Matrix
{
private:
int **m;
public:
Matrix();
void print();
void transpose();
Matrix *transpose();
~Matrix();
}
Access Control
Constructor/Destructor
Member Functions
– Ordinary Functions
– Overloaded Functions
![Page 20: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/20.jpg)
Constructors
Different Constructors for a class
– Matrix();
– Matrix(int init); // 0 = zero matrix, 1 = Identity!!
– Matrix(Matrix m);
Writing a constructor
![Page 21: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/21.jpg)
Objects
Creation
– Named object, free-store object (new & delete),
non-static member object, an array element,
temporary object, …etc
Destruction
Copying Objects
– Direct assignment
– Copy Constructors - Matrix::Matrix(Matrix &m);
![Page 22: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/22.jpg)
Creating Matrix Objects
Matrix m;
Matrix *m = new Matrix();
Matrix *n = m;
Matrix *n = new Matrix(m);
Matrix ma[10];
Matrix *mp = new Matrix[10];
Matrix *np = new Matrix[10](m);
![Page 23: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/23.jpg)
Interacting with Matrix Objects
Matrix m;
m.transpose();
Matrix *n = m.transpose();
![Page 24: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/24.jpg)
Functions
Static functions
Constant functions and mutable
Inline functions
Helper functions
![Page 25: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/25.jpg)
Operators
Member and Non-Member Operators
Unary
Binary
– Matrix & operator+= (Matrix a) // member
– Matrix Matrix::operator + (Matrix b) // member
– Matrix operator + (Matrix a, Matrix b) //non-member
![Page 26: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/26.jpg)
A Code Example
class Complex
{
private:
int real;
int imaginary;
public:
Complex(int r, int i= 0): real(r),imaginary(i);
Complex(const Complex& a);
int re() const;
int im() const;
void operator += (Complex a);
Complex operator + (Complex b);
void print();
};
![Page 27: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/27.jpg)
The Code..
Complex(const Complex& a)
{
real = a.real;
imaginary = a.imaginary;
}
int re() const
{
return real;
}
int im() const
{
return imaginary;
}
![Page 28: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/28.jpg)
The Operator Code
void operator += (Complex a)
{
real += a.re();
imaginary += a.im();
}
Complex operator + (Complex b)
{
Complex nc(*this);
nc += b;
return nc;
}
![Page 29: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/29.jpg)
Inheritance
What is inheritance ?
Parent and Derived
Some properties of derived classes
– Constructors
– Member functions
– Overloading
![Page 30: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/30.jpg)
Inheritance in C++
Class Rectangle : public Shape
{
}
Class Student : public Person
{
}
Public, protected and private !!
![Page 31: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/31.jpg)
Multiple Inheritance
class Transmitter {
…
}
Class Receiver {
…
} Class Radio : public Transmitter, public Receiver
![Page 32: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/32.jpg)
Abstract Classes
class Shape
{
public:
virtual void rotate ( int )=0; // Make it pure !!
virtual void draw()=0;
}
Polymorphism through virtual functions ?
![Page 33: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/33.jpg)
Virtual Base Classes
Base Base
| |
Derived1 Derived2
|__ Derived3 __|
class Derived1 : virtual public Base
class Derived2 : virtual public Base
class Derived3 : public Derived1, public Derived2
![Page 34: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/34.jpg)
Member Access Table
Derived with Private Base Protected
Base
Public Base
Private
Variables
X X X
Protected
Variables
private protected Protected
Public
Variables
private protected Public
![Page 35: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/35.jpg)
Templates And Generic Programming
Generic Functions and Classes
template <class X> FUNCTION_NAME()
Example :
– template <class X> void swap(X &a, X &b);
– template <class X, class Y>
void function(X &a, Y &b)
![Page 36: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/36.jpg)
An example
template <class data_type> class List
{
data_type data;
List *next;
public:
List(data_type d);
data_type getdata();
}
![Page 37: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/37.jpg)
Creating Template Objects
A list of integers List<int> li;
A list of doubles List<double> ld;
A list of strings List<string> ls;
A list of addresses List<address> ls;
– (where address is a predefined class/structure)
![Page 38: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/38.jpg)
C++ I/O
The <iostream>
using namespace std;
cout, cin, cerr
ifstream, ofstream and fstream
Ifstream in;
in.open(const char *filename,openmode mode)
Mode can be ios:in, ios;out, ios:app,ios:binary,…etc
![Page 39: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/39.jpg)
File Reading and Writing
ifstream in;
double num;
char str[20];
in.read((char *)&num, sizeof(double));
in.read(str,10);
Using << and >> Operators.
in.getline();
![Page 40: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/40.jpg)
The Standard Template Library
#include <vector>
vector <int> v;
Insertion, Access and Deletion: push_back,pop_back,insert,front,size,..(see the manual)
Iterators vector<int>::iterator p = v.begin();
Other Templates :: List, Deque, Map….etc!!
![Page 41: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/41.jpg)
Basic Exception Handling
Try
Throw
Catch
![Page 42: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/42.jpg)
Syntax
try
{
throw something;
}
catch (something)
{
// Do whatever has to be done here.
}
catch (…) :: Catches everything.
![Page 43: Object Oriented Programming using C++€¦ · Object Oriented Programming using C++ . Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms . Solving a Programming](https://reader033.fdocuments.us/reader033/viewer/2022052423/5f0abc897e708231d42d1755/html5/thumbnails/43.jpg)
Syntax
try
{
throw something;
}
catch (something)
{
// Do whatever has to be done here.
}
catch (…) :: Catches everything.