CPP
CPP
CPP
Overview
A rectangle uses lines A circle is an ellipse A wheel is part of automobile A set creates its elements
Easy Representation of
Interaction with objects of same type Relations with objects of other type Polymorphism and Overloading
Basic Syntax
Programming Paradigms
Procedural Programming (functions) Modular Programming (namespaces) Object Oriented Programming (classes) Generic Programming (templates)
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 !!
Modular Programming
Matrix Module
Vector Module
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); };
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"); }
void Vector::print(int *vector,int size) { for (int i = 0; i < size; i++) printf("%d ",vector[i]); printf("\n"); }
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. }
Class
Access Control
Constructors
Writing a constructor
Objects
Creation
Named object, free-store object (new & delete), non-static member object, an array element, temporary object, etc
Functions
Static functions Constant functions and mutable Inline functions Helper functions
Operators
Matrix & operator+= (Matrix a) // member Matrix Matrix::operator + (Matrix b) // member Matrix operator + (Matrix a, Matrix b) //non-member
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(); };
The Code..
Complex(const Complex& a) { real = a.real; imaginary = a.imaginary; } int re() const { return real; } int im() const { return imaginary; }
Inheritance
Inheritance in C++
Class Rectangle : public Shape { } Class Student : public Person { } Public, protected and private !!
Multiple Inheritance
class Transmitter { } Class Receiver { }
Class Radio : public Transmitter, public Receiver
Abstract Classes
class Shape { public: virtual void rotate ( int )=0; // Make it pure !! virtual void draw()=0; } Polymorphism through virtual functions ?
Derived3
class Derived1 : virtual public Base class Derived2 : virtual public Base class Derived3 : public Derived1, public Derived2
Derived with Private Base Protected Base Private Variables Protected Variables X private X protected
Public Variables
private
protected
Public
template <class X> void swap(X &a, X &b); template <class X, class Y> void function(X &a, Y &b)
An example
template <class data_type> class List { data_type data; List *next; public: List(data_type d); data_type getdata(); }
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;
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
Syntax
try { throw something;
Syntax
try { throw something;