C++ Classes & Object Oriented Programming

Similar documents
AN OVERVIEW OF C++ 1

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

private: // can only be used by the member functions of MyType and friends of MyType };

Interview Questions of C++

Fast Introduction to Object Oriented Programming and C++

Chapter 10 Introduction to Classes

Data Structures using OOP C++ Lecture 3

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

The 10 Minute Guide to Object Oriented Programming

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Lecture 18 Tao Wang 1

Short Notes of CS201

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

CS201 - Introduction to Programming Glossary By

Standard. Number of Correlations

Abstraction in Software Development

Object-Oriented Programming

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Introduction to Classes

Inheritance, and Polymorphism.

Object Oriented Design

Software Design and Analysis for Engineers


Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming

Chapter 1: Object-Oriented Programming Using C++

OOPs: The Harsh Realities of Programming

Introduction to Programming using C++

Lab 12 Object Oriented Programming Dr. John Abraham

Important when developing large programs. easier to write, understand, modify, and debug. each module understood individually

Unit 1 : Principles of object oriented programming

JAVA GUI PROGRAMMING REVISION TOUR III

Lecture 04 FUNCTIONS AND ARRAYS

Function Overloading

Object-Oriented Programming

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

Classes Ch

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Object oriented programming Concepts

EL2310 Scientific Programming

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Chapter 15: Object Oriented Programming

Dr. Md. Humayun Kabir CSE Department, BUET

Programming in C/C Lecture 3

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Object-Oriented Programming

Fundamentals of Programming. Lecture 19 Hamed Rasifard

type conversion polymorphism (intro only) Class class

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

Object Oriented Design

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

JAVA: A Primer. By: Amrita Rajagopal

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

C++ Important Questions with Answers

Polymorphism. Arizona State University 1

M.CS201 Programming language

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

Overview CSE 142. Naming Revisited Declarations. Defining Parts of Objects

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Lecture #1. Introduction to Classes and Objects

Programming, numerics and optimization

Variables. Data Types.

Midterm Review. PIC 10B Spring 2018

Object Oriented Programming. Solved MCQs - Part 2

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Absolute C++ Walter Savitch

Cpt S 122 Data Structures. Introduction to C++ Part II

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

OBJECT ORIENTED PROGRAMMING

2. Functions I: Passing by Value

A SHORT COURSE ON C++

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

CS 61B Discussion Quiz 1. Questions

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Programming Abstractions

Module 10 Inheritance, Virtual Functions, and Polymorphism

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Introduction Of Classes ( OOPS )

Chapter 02 Building Multitier Programs with Classes

EL2310 Scientific Programming

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Classes. OOP & Class Object Terminology File Management

Programming for Engineers in Python

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

What is a class? Responding to messages. Short answer 7/19/2017. Code Listing 11.1 First Class. chapter 11. Introduction to Classes

COEN244: Class & function templates

XII CS(EM) Minimum Question List N.KANNAN M.Sc., B.Ed COMPUTER SCIENCE IMPORTANT QUESTION (TWO MARKS) CHAPTER 1 TO 5 ( STAR OFFICE WRITER)

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

A brief introduction to C++

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Java Object Oriented Design. CSC207 Fall 2014

PIC 10A Objects/Classes

WYSE Academic Challenge Computer Science Test (Regional) 2015 Solution Set

C++ Programming Lecture 7 Software Engineering Group

Introduction to Programming

Increases Program Structure which results in greater reliability. Polymorphism

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Transcription:

C++ Classes & Object Oriented Programming What is it? Object Oriented Programming 1

Object Oriented Programming One of the first applications of modern computing was modeling and simulation. Scientists soon realized that functions alone were insufficient to model systems intuitively If we are going to model a planet we would like to actually create a virtual planet, define how it behaves in our simulated universe, and then just observe it. Object Oriented Programming 2

Object Oriented Programming Programmers quickly realized that the idea of creating virtual things made software engineering simpler to think about. If we create within our programs agents and objects then we can assign duties and tasks to them. This is really just another way applying decomposition to our software. Break up the problem to be solved into logical parts and assign each part to an object. Object Oriented Programming 3

Object Oriented Programming Even engineers are social animals - we evolved to think about the world in terms of agents and objects (not recursion). In many situations we solve large problems by delegation. That is we have workers who specialize in solving a particular problem. Those specialists have specific skills that they can apply to a specific class of problems. Object Oriented Programming 4

Object Oriented Programming We can pattern software after a group of specialists at a company working on a problem. For example, there are two objects we have used cin and cout. cin is the name of an object who knows all about reading data from the keyboard and putting it into a variable. It is easier to ask cin to do the work than write a program to do it ourselves. Object Oriented Programming 5

Object Oriented Programming Important: we don t have to have any idea how cin does its job. We just trust that it does. Just like we don t question the US Mail about how our letter gets from here to Seattle. We only care that it arrives within certain tolerances not how it got there. This is called abstraction, informationhiding, and encapsulation and we like it! Object Oriented Programming 6

Object Oriented Programming When we mail a letter all we have to worry about is following the correct protocol to ensure our letter gets to the right place. We have to know where to go, how to pay, the format expected for the destination address and return address, etc. In software this protocol is called the interface. All objects have to have an interface that clearly defines how we can interact with the object. Object Oriented Programming 7

Object Oriented Programming Almost any problem can be broken up into objects. Objects are defined by three things: Their state this is the information they contain. Their behavior or capabilities these are the functions they have access to. Their interface the rules describing how they interact with other objects in the system. Object Oriented Programming 8

Object Oriented Programming Programmer thinks about and defines the attributes and behavior of objects. Often the objects are modeled after realworld entities. Very different approach than function-based programming (like C). Object Oriented Programming 9

Reasons for OOP Abstraction Encapsulation Information hiding Inheritance Polymorphism Software Engineering Issues Object Oriented Programming 10

Class: Object Types C++ uses classes and structures to define objects A C++ class is an object type. When you create the definition of a class you are defining the attributes and behavior of a new type. Attributes are data members. Behavior is defined by methods. Object Oriented Programming 11

Creating an object The interface acts as a contract specifying how the object will behave as long as the code fulfills the contract we don t care how it works. Defining a class does not result in creation of an object. Declaring a variable of a class type creates an object. You can have many variables of the same type (class). This is called instantiation of the class, i.e. we create an instance of the object. Object Oriented Programming 12

Information Hiding The interface to a class is the list of public data members and methods. The interface defines the behavior of the class to the outside world (to other classes and functions that may access variables of your class type). The implementation (the code that makes the class work) doesn't matter outside the class. Object Oriented Programming 13

Information Hiding (cont.) This is good because it allows us to change the underlying code without forcing everyone who uses our objects to change their code. You can change the implementation and nobody cares! (as long as the interface is the same). Object Oriented Programming 14

Private vs. Public Classes define certain parts of the object they define to be public, private, or protected. Public parts of the object can be used by anyone who has access to the object. The private parts of the object are for the objects internal use only. Protected parts are accessible from outside the object only under certain circumstances. Try to make as much private as possible. Object Oriented Programming 15

Special Member Functions Constructors: called when a new object is created (instantiated). can be many constructors, each can take different arguments Destructor: called when an object is destroyed only one, has no arguments. The destructor is responsible for cleaning up after the object Object Oriented Programming 16

Anatomy of a Class Class Definition (function prototypes) class Dog { public: Dog( char* dog_name = rover ); bark(); ~Dog(); char* name; private: }; Object Oriented Programming 17 Put all this in Dog.h

Class Implementation (function definitions) #include Dog.h using namespace std; Dog::Dog( char* dog_name) { name = dog_name; } Dog::bark() { cout << woof ; } Dog::~Dog() {//nothing to do} Put all this in Dog.cpp Object Oriented Programming 18

#include Dog.h int main() { Using a Class and an Obeject char my_dogs_name = fido ; // Create object of type Dog Dog mydog( my_dogs_name ); // Access data and call methods in mydog cout << mydog.name << : ; mydog.bark(); } return 0; Object Oriented Programming 19

Accessing Data Members Data members are available within each method (as if they were local variables). Public data members can be accessed by other functions using the member access operator ".". Object Oriented Programming 20

Accessing class methods Within other class methods, a method can be called just like a function. Outside the class, public methods can be called only when referencing an object of the class. Object Oriented Programming 21

Classes and Files The relationship between C++ class definitions and files depends on the compiler. In general you can put class definitions anywhere! Visual C++ wants one class per file. Most people do this: class definition is in classname.h any methods defined outside of the class definition are in classname.cpp Object Oriented Programming 22

Classes and Files Now that we are working with multiple source (.cpp) and header files (.h) we need to be more sophisticated about compiling. Each source file is compiled separately into object files. These object files cannot be run independently they have to be linked into a single executable program file. Unix systems use the make command to organize compilation and linking. Object Oriented Programming 23