Object Oriented Programming using C++. Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
C++ Classes & Data Abstraction
C/c++ 4 Yeting Ge.
EEM 480 Algorithms and Complexity by Assist. Prof. Dr. Emin Germen.
C++ fundamentals.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Distribution of Marks Internal Sessional Evaluation Assignments – 10 Quizzes – 10 Class Participation Attendence – 5 Mid – Term Test – 25 External Evaluation.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Object Oriented Programming using VC++. Introduction Program – Set of instruction written in a high level language High level language used for writing.
C++ Yeting Ge.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ C++ Overview (I) What is Object Orientated Programming? Approach: Break problem into subgroups of related parts that take into account code and data;
Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Java and C++, The Difference An introduction Unit - 00.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 11 and 12: Abstract Data Types and Encapsulation Constructs; Support for Object Orientation Lesson 11.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Data Structure and Algorithm: CIT231 Lecture 4: ADT and Introduction to Object Oriented Programming (OOP) DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
CS 11 C++ track: lecture 7 Today: Templates!. Templates: motivation (1) Lots of code is generic over some type Container data types: List of integers,
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Programming Language C++ Xulong Peng CSC415 Programming Languages.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Basic Concepts of OOP in C++ Darvay Zsolt. C++ 2 Outline  The namespace and its members  The using declaration and directive  The address operator.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
Hank Childs, University of Oregon May 13th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
Chapter 10 Inheritance and Polymorphism
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
1.What are the differences between composition(“has a” relationship) and inheritance(“is a” relationship)? 2.What is a base class? 3.How to declare a derived.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Object-Oriented Programming (OOP) and C++
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Chapter 2 Objects and Classes
Hank Childs, University of Oregon
Introduction to Computers Computer Generations
C++ INTERVIEW QUESTIONS
Object-Oriented Programming (OOP) Lecture No. 45
CS3340 – OOP and C++ L. Grewe.
Review: Two Programming Paradigms
Chapter 5 Classes.
Object-Orientated Programming
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
CPS120: Introduction to Computer Science
Final Exam Review Inheritance Template Functions and Classes
Presentation transcript:

Object Oriented Programming using C++

Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms

Solving a Programming Problem Analysis Design Coding Management Programming paradigms Programming languages

Concepts & Relationships – A rectangle uses lines – A circle is an ellipse – A wheel is part of automobile – A set creates its elements

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

Basic Syntax Same as C Additional operators Additional keywords

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 namespace Example to learn the syntax : A toy math library. – Matrix Module Allocate Transpose Print – Vector Module Allocate Transpose Print

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); };

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"); }

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"); }

How to use Matrix and Vector ? // in the file main.c #include 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); }

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); }

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. }

Using the transpose() // in the file main.c #include 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); }

Object Oriented Programming Class Object Overloading (operators, functions) Inheritance

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

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

Constructors Different Constructors for a class – Matrix(); – Matrix(int init); // 0 = zero matrix, 1 = Identity!! – Matrix(Matrix m); Writing a constructor

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);

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);

Interacting with Matrix Objects Matrix m; m.transpose(); Matrix *n = m.transpose();

Functions Static functions Constant functions and mutable Inline functions Helper functions

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

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; }

The Operator Code void operator += (Complex a) { real += a.re(); imaginary += a.im(); } Complex operator + (Complex b) { Complex nc(*this); nc += b; return nc; }

Inheritance What is inheritance ? Parent and Derived Some properties of derived classes – Constructors – Member functions – Overloading

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 ?

Virtual Base Classes Base | Derived1Derived2 |__Derived3 __| class Derived1 : virtual public Base class Derived2 : virtual public Base class Derived3 : public Derived1, public Derived2

Member Access Table Derived withPrivate BaseProtected Base Public Base Private Variables XXX Protected Variables privateprotectedProtected Public Variables privateprotectedPublic

Templates And Generic Programming Generic Functions and Classes template FUNCTION_NAME() Example : – template void swap(X &a, X &b); – template void function(X &a, Y &b)

An example template class List { data_type data; List *next; public: List(data_type d); data_type getdata(); }

Creating Template Objects A list of integers List li; A list of doubles List ld; A list of strings List ls; A list of addresses List ls; – (where address is a predefined class/structure)

C++ I/O The 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

File Reading and Writing ifstream in; double num; char str[20]; in.read((char *)&num, sizeof(double)); in.read(str,10); Using > Operators. in.getline();

The Standard Template Library #include vector v; Insertion, Access and Deletion: push_back,pop_back,insert,front,size,..(see the manual) Iterators vector ::iterator p = v.begin(); Other Templates :: List, Deque, Map….etc!!

Basic Exception Handling Try Throw Catch

Syntax try { throw something; } catch (something) { // Do whatever has to be done here. } catch (…) :: Catches everything.

Syntax try { throw something; } catch (something) { // Do whatever has to be done here. } catch (…) :: Catches everything.