C++ Classes & Object Oriented Programming

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
C++ Classes & Data Abstraction
Classes and Objects Systems Programming.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 Writing a Good Program 5. Objects and Classes in C++
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
C++ fundamentals.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
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.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,
Abstraction ADTs, Information Hiding and Encapsulation.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
1 CSC241: Object Oriented Programming Lecture No 02.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
Pointer to an Object Can define a pointer to an object:
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Procedural and Object-Oriented Programming
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
Andy Wang Object Oriented Programming in C++ COP 3330
Classes and Objects in Java
Review: Two Programming Paradigms
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Introduction to Classes
Classes and Data Abstraction
IFS410: Advanced Analysis and Design
Object-Oriented Programming
Week 3 Object-based Programming: Classes and Objects
Introduction to Classes and Objects
Object-Oriented Programming
CISC/CMPE320 - Prof. McLeod
CLASSES, AND OBJECTS A FIRST LOOK
Object-Oriented Programming
NAME 436.
Object-Oriented PHP (1)
CPS120: Introduction to Computer Science
Final and Abstract Classes
Classes and Objects in Java
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
C++ Object Oriented 1.
UML  UML stands for Unified Modeling Language. It is a standard which is mainly used for creating object- oriented, meaningful documentation models for.
CSG2H3 Object Oriented Programming
Introduction to Classes and Objects
Presentation transcript:

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

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

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

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

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

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, information- hiding, and encapsulation and we like it! Object Oriented Programming

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

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

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

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

Object Oriented Programming 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

Object Oriented Programming 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

Object Oriented Programming 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

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

Object Oriented Programming 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

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

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

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

Using a Class and an Obeject #include “Dog.h” int main() { 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

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

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

Object Oriented Programming 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

Object Oriented Programming 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