CSC241: Object Oriented Programming

Slides:



Advertisements
Similar presentations
Overview of Data Structures and Algorithms
Advertisements

1 Object-Oriented Programming OOP. 2 Object-oriented programming is a new paradigm for program development in which the focus is on the data instead of.
BITS Pilani Avinash Gautam Department of Computer Science and Information Systems.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
What is an object? Your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior;
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Pertemuan 6 Object Oriented Programming Matakuliah: T0053/Web Programming Tahun: 2006 Versi: 2.
Object Oriented Software Development
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Object Oriented Programming Concepts Fatih University Ceng-104-A Introduction to Object Oriented Programming Harun Reşit Zafer This is a slide version.
Object Oriented Programming CS160 - OOP. Objects Objects are a Objects are a way to organize and conceptualize a program as a set of interacting objects.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Java How to Program, Late Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Learners Support Publications Classes and Objects.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 1.
CS1201: Programming Language 2 Classes and objects By: Nouf Aljaffan Edited by : Nouf Almunyif.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Object Oriented Programming Concepts. Object ► An object is a software bundle of related state and behavior. ► Software objects are often used to model.
CSCI 3327 Visual Basic Chapter 3: Classes and Objects UTPA – Fall 2011.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Data Structures and Algorithms Dr. Tehseen Zia Assistant Professor Dept. Computer Science and IT University of Sargodha Lecture 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
1 CSC241: Object Oriented Programming Lecture No 02.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Methodology First and Language Second -A Way to Teach Object-Oriented Programming Haibin Zhu, PhD Department of Computer Science and Mathematics Nipissing.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
CMSC 345 Fall 2000 OO Design. Characteristics of OOD Objects are abstractions of real-world or system entities and manage themselves Objects are independent.
1 CSC241: Object Oriented Programming Lecture No 03.
Lecture #6 Classes and Objects.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 1 Introduction to Object-Oriented Programming and.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Object-Oriented Paradigm (OOP) Course Code: SE 101 Lecture No. 1.
Object Oriented Paradigm OOP’s. Problems with Structured Programming As programs grow ever larger and more complex, even the structured programming approach.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Chapter 3 Implementing Classes
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Dr Shahriar Bijani Winter 2017
Chapter 3 Introduction to Classes, Objects Methods and Strings
Object-Oriented Programming
Introduction to Classes and Objects
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Object-Oriented Programming
Classes, Objects, Methods and Strings
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Object-Oriented Programming
Object Oriented Programming in java
(4 – 2) Introduction to Classes in C++
Introduction to Classes and Objects
Presentation transcript:

CSC241: Object Oriented Programming Lecture No 01

Aim of the course This course provides motivation of object oriented programming language in depth knowledge of the various concepts of object oriented programming and its implementation in C++

Course book Text book Reference books C++ How to program by Deitel and Deitel Reference books Waite Group’s Object oriented programming in C++, Robert Lafore

Course outline Classes, Objects, Member functions, Objects as data types, Constructors and destructors, Overloaded constructor The default copy constructor, Returning objects from functions, objects and memory, Static class data, Constant member functions, Constant objects Base classes and derived classes, Derived class constructors, Overloading member functions, Scope resolution, Abstract classes, Public and private inheritance, Levels of inheritance, Multiple inheritance, Aggregation and composition New and delete operators, Pointers to objects, Virtual functions and late binding, Abstract classes and pure virtual functions, Virtual destructors, Virtual base classes, Friend functions and friend classes, Static functions, this pointer, Dynamic type information Motivation for exception handling, Try-catch block, Throwing an exception, Catching multiple exceptions Streams and files, Templates

Marks distribution Assignments: 10% Quizzes: 15% Sessional exam 01: 10% Sessional exam 02: 15% Terminal Exam: 50%

Introduction Five concepts in object oriented programming are: Object Classes Encapsulation Inheritance Polymorphism

Simple analogy You are driving a car You can pressing accelerator pedal Someone has design it and built it Engineering drawings  car Drawings also includes design for accelerator pedal to make car go faster We can say, pedal “hides” complex mechanism that make the car go faster

Cont. Brake pedal “hides” the mechanism that slow the car Steering wheel “hides” the mechanism that turn the car and so on Simple “interfaces” like accelerator and brake pedal, steering wheel, transmission shift and etc. allow driver to interact car’s complex internal mechanisms

Points to be noted You cannot drive the engineering design of a car Before you can drive a car, it must be built according to engineering design The car will not accelerator on its own, a driver must press the accelerator pedal

Object oriented programming concepts Function hides from user the complex task it performs Same as accelerator pedal hides complex mechanism of making the car go faster C++ makes a program unit called class that houses various functions Same as car engineering design houses the mechanism of accelerator pedal

Cont. In C++, a class can have various functions that are design to perform a class tasks For example, a class representing bank account might contain functions Deposit money Withdraw money Current balance

Car example Real world Engineering drawing cannot be drive A car is build from that drawing Pressing accelerator pedal sends a message to car to perform task (go faster) C++ programming An object of a class must be create to get a program to perform the tasks the class describes Message can be sent to object by calling a member functions

Cont. Car analogy is used to introduce Class Objects Member functions In addition to capabilities of car, it has many attributes Color, No. of doors, amount of gas in tank, total miles driven and etc Attributes are part of car engineering drawing

Cont. These attribute are always associated with the car Every car maintains its own attribute Example 1: each car knows how much gas in its own tank but do not know how much is in the tanks of other cars Example 2: a bank account object has a balance attribute. Each bank account object knows the balance in its account not the others

Object Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have  State and Behavior

Object example A dog x has state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Your bicycle also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes).

Cont. For each object that you see, ask yourself two questions: "What possible states can this object be in?" and "What possible behavior can this object perform?"

Real world objects Real-world objects vary in complexity your desktop lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune).

Cont.. You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming

Class In the real world, you'll often find many individual objects all of the same kind There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same engineering design and contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.

Software Object Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in  fields (variables in some programming languages) and exposes its behavior through  methods (functions in some programming languages). A Software Object

Cont. Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.

Class vs. Object Class is a blue print of an object, which is non-live entity. Object is instance of class, which is a live entity. Example: Employee is a class Fruit is a class I am an object apple is an object

Points to remember A class is not a living entity, it is just a engineering design that how an object of this class look like Object are living entities

Defining a class with member function

Cont. Class definition Access specifier – Public Class’s body is enclosed in a pair of { } Class definition ends at semi colon Member function Class object Dot operator

Member function with parameter Write a program

Book Class Simple program #include <iostream.h> #include <string.h> class book{ private: char name[25]; int pages; float price; public: void changeName(char *n){ strcpy(name, n); } void changePages(int p){ pages = p; void changePrice(float p){ price = p; void display(){ cout<<"name = "<<name<<" pages = "<<pages<<" price = "<<price<<endl; }; Book Class Simple program

Class data The class book contain three data items char name[15]; int pages; float price; There can be any number of data members in a class just as in structure There data member lie under keyword private, so they can be accessed from within the class, but not outside

Member function These functions are included in a class There are four member functions in class book changeName(char *n) changePages(int p) changePrice(float p) display() There functions are followed by a keyword public, so they can be accessed outside the class

Class data and member function Access specifier label public and private Function are public and data is private Data is hidden so that it can be safe from accidental manipulation Functions operates on data are public so they can be accessed from outside the class

Defining Objects b1 500 150.56 Name Pages Price Operating system void main() { book b1; b1.changeName("Operating System"); b1.changePages(500); b1.changePrice(150.56); b1.display(); } Name Pages Price b1 Operating system 500 150.56

Cont. Defining an object is similar to defining a variable of any data type: Space is set aside for it in memory e.g. int x; Defining objects in this way (book b1;) means creating them, also called instantiating them An object is an instance (that is, a specific example) of a class. Objects are sometimes called instance variables.

Calling Member Functions The next four statements in main() call the member function b1.changeName("Operating System"); b1.changePages(500); b1.changePrice(150.56); b1.display(); don’t look like normal function calls This syntax is used to call a member function that is associated with a specific object It doesn’t make sense to say changeName("Operating System"); because a member function is always called to act on a specific object, not on the class in general

Cont. To use a member function, the dot operator (the period) connects the object name and the member function. The syntax is similar to the way we refer to structure members, but the parentheses signal that we’re executing a member function rather than referring to a data item. The dot operator is also called the class member access operator.

Data members, set and get functions

Example program – Distance class Data members Feet Inches Member functions void setdist(int ft, float in); void getdist(); void initialize(); void showdist(); Go to program

Constructors The Distance example shows two ways that member functions can be used to give values to the data items in an object It is convenient if an object can initialize itself when it’s first created, without requiring a separate call to a member function Automatic initialization is carried out using a special member function called a constructor. A constructor is a member function that is executed automatically whenever an object is created.

A counter example Data member Member function Count Constructor void inc_count() int get_count() Go to program