Section 6 - Object-Oriented C++ - Classes. The idea of OO programming is to model the features (aka properties or attributes) and behaviour of real-world.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Section 5 – Classes. Object-Oriented Language Features Abstraction –Abstract or identify the objects involved in the problem Encapsulation –Packaging.
Function Overloading Having more than one function with the same name. list of argument of a function are called signature of a function. We can have more.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
C++ fundamentals.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
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.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
An Object-Oriented Approach to Programming Logic and Design
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 5 Classes and Objects §5.1 Specifying a class §5.2 Defining Member Functions §5.3 Memory Allocation and Static Members §5.4 Passing Objects to/from.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved Chapter Nine: Classes Slides by Evan Gallagher.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
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.
Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Matthew Small Introduction to Object-Oriented Programming.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
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.
Chapter 3 Implementing Classes
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Classes (Part 1) Lecture 3
Review: Two Programming Paradigms
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Introduction to Classes
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Introduction to Classes
Chapter 9 Objects and Classes
Object-Oriented Programming
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

Section 6 - Object-Oriented C++ - Classes

The idea of OO programming is to model the features (aka properties or attributes) and behaviour of real-world objects in the problem domain by software objects (aka instances). The class construct provides a template (or blueprint) for the creation of objects. Classes specify what attributes and behaviour an object may have. May create many objects from a given class - Each object will have its own attribute, but will have identical behaviour.

Important Concepts for OO Programming Abstraction Extract only the relevant properties of a real-world ofor developing a class while ignoring the inessentials Encapsulation Group the attributes and behaviour of an object together in a single data structure known as a class Information Hiding Hide and protect essential information of the class from outside functions through a controlled interface

For any problem, extract the relevant real-world object properties for software objects, while ignoring inessentials – Defines a view of the software object Example - car – Car dealer views a car from selling features standpoint Price, length of warranty, color, … – Mechanic views a car from systems maintenance standpoint Size of the oil filter, type of spark plugs, … OO Feature - Abstraction

Encapsulation and Information Hiding Steps –Decompose an object into parts –Hide and protect essential information –Supply an interface that allows an object to be accessed in a controlled and useful manner Interface means that the internal representation of a class can be changed without affecting other system parts Example - Radio –Interface consists of controls and power and antenna connectors The details of how it works is hidden –To install and use a radio Do not need to know anything about the radio’s electronics

OO Feature - Modularity Dividing an object into smaller pieces or “modules” so that the object is easier to understand and manipulate. Most complex systems are modular Example - Car can be decomposed into subsystems – Cooling system Radiator ThermostatWater pump – Ignition system Battery Starter motorSpark plugs

OO Feature - Hierarchy Hierarchy – Ranking or ordering of objects based on some relationship between them Helps us understand complex systems – Example - a company hierarchy helps employees understand the company and their positions within it For complex systems, a useful way of ordering similar abstractions is a taxonomy from least general to most general

Inheritance Hierarchies

The 3 Main OOP Characteristics Data Encapsulation and Information Hiding –Data and functions are said to be encapsulated into a single entity – the class –Data is concealed within a class, so that it cannot be accessed mistakenly by functions outside the class. Inheritance Polymorphism

The 3 Main OOP Characteristics Data Encapsulation and Information Hiding Inheritance –The process of creating new classes, called derived classes, from existing classes or base classes creating a hierarchy of parent classes and child classes. –The child classes inherit the attributes and behaviour of the parent classes. Polymorphism

The 3 Main OOP Characteristics Data Encapsulation and Data Hiding Inheritance Polymorphism –Generally, the ability to appear in many forms –More specifically, in OOP, it is the ability to redefine methods for derived classes –Ability to process objects differently depending on their data type or class –Giving different meanings to the same thing

Class Construct Allows the definition of specific objects with defined attributes (data) and behaviour (functions) → Essence of object-oriented programming

Classes Classes are language constructs that allow the definition and creation of objects of the same type. A class uses variables to define data fields and functions to define behaviours. Additionally, a class provides a special type of function, known as a constructor, which is invoked to create new objects from the class definition.

A class describes a set of objects with the same behavior. You may create the Car class to represent cars as objects To define a class, you must specify the behaviour by providing implementations for the member functions, and by defining the data members for the objects …

Classes

#include using namespace std; class Circle { private: // The radius of this circle double radius; public: // Construct a default circle object Circle() { radius = 1; } // Construct a circle object Circle(double newRadius) { radius = newRadius; } // Return the area of this circle double getArea() { return radius * radius * ; } }; // Must place a semicolon here int main() { Circle circle1(1.0); Circle circle2(25); Circle circle3(125); cout << "The area of the circle of radius " " 1.0 is " << circle1.getArea() << endl; cout << "The area of the circle of radius " “25 is " << circle2.getArea() << endl; cout << "The area of the circle of radius " “125 is " << circle3.getArea() << endl; return 0; }

OO Feature - Objects An object is almost anything with the following characteristics – Name – Properties – Operations – The ability to act upon receiving a “message” – being called by a function or another object. Basic message types – Directive to object to perform an action – Request to object to change one of its properties

Class Data Types The Class construct - Actually allows programmers to define new data types for representing information - Class type objects can have both attribute and behaviour components - Provides the object-oriented programming in C++

Terminology Object behaviours - Realized in C++ via member functions (aka methods) - Methods are public – accessible from outside the class Object attributes - Are known as data members in C++ - Attributes are private – only accessible to class members

Any part of the program should be able to call the member functions – so they are in the public section. Data members are defined in the private section of the class. - Only member functions of the class can access them. - They are hidden from the rest of the program. So that outside functions may access the data members, we provide special functions in the class to form an interface – accessors and mutators.

Object Names You assign an object a name when creating the object. A special class function called the class constructor is invoked when an object is created. The syntax to create an object using the constructor is ClassName objectName; For example, Circle circle1; for some class Circle Note that a constructor has the same name as the class.

Access Operator After an object is created, its data can be accessed and its functions invoked using the dot operator. objectName.dataField references a data field of the object. objectName.function(arguments) invokes a function of the object.

Private versus Public Public member functions allow “clients” to operate on the objects of a class. May have private member functions –If member functions need to call another function that is part or not part of the class then it should be private Use care when defining public access

Information hiding recommends that data members should be private - not freely accessible by clients So, in order for clients to read/write the values of the data members of an object, must provide so-called get and set functions - get – read value - set – write value The only way clients may access the data members.

Accessor and Mutator Colloquially, a get function is referred to as a getter (or accessor), and a set function is referred to as a setter (or mutator). A get function has the following signature (Only a convention!): returnType getPropertyName() A set function has the following signature (only a convention!): public void setPropertyName(dataType propertyValue)

Classes class NameOfClass { public: // The “public” interface – available to clients // Here we define the public methods of the class private: // the data members – only accessible by functions of the class };

Remember Every class object - Has its own data members - Has its own member functions (which are the same as other objects of the same class have) - When a member function accesses a data member By default the function accesses the data member of the object to which it belongs! - No special notation needed

Next three slides illustrate the conventional concept of classes including general methods like getData() and showData().

class Person { private: string name; int age; public: void setData() { cout > name; cout > age; } void getData() { cout << “\nName:” << name << ”\t\tAge:” << age; } }; No constructor here!

// Statements to define instances of the Person class: // Objects named Ivan and Elena Person Ivan, Elena; // Array of Person objects named family with size of 5 Person family[5];

// Statements to call members (data and methods) of the Person class: // Private members cannot be called from outside the class // Public members can be called from inside and outside the class Ivan.setData(); Ivan.getData(); // OK Ivan.age = 19; // Error

Example – better! class Person { private: string name; unsigned age; public: string getName() { return name; } unsigned getAge() { return age; } void setName(string par){name = par;} void setAge(unsigned par){age = par;} };

Constructors Special member function –Same name as class name Constructor is invoked each time an object is declared for a given class Initializes object

Constructor The constructor has exactly the same name as the defining class. A class normally provides a constructor without arguments - default constructor. Like regular functions, constructors can be overloaded (i.e. multiple constructors with the same name but different signatures), making it easy to construct objects with different initial data values. A class may be declared without constructors. - In this case, a default constructor, is provided automatically but only if no constructors are explicitly declared in the class.

Constructing with Arguments Constructors may be overloaded The syntax to declare an object using a constructor with parameters is ClassName objectName(parameters); For example, the following declaration creates an object named circle2 by invoking the Circle class’s constructor with a specified radius 5.5. Circle circle2(5.5);

To define a class you write: Classes class NameOfClass { public: // the public interface private: // the data members };

Any part of the program should be able to call the member functions – so they are in the public section. Data members are defined in the private section of the class. Only member functions of the class can access them. - They are hidden from the rest of the program. class NameOfClass { public: // the public interface private: // the data members };

Here is the C++ syntax for a CashRegister class definition: class CashRegister { public: void clear(); void add_item(double price); double get_total() const; int get_count() const; private: // data members will go here };

The public interface has the four activities that we decided this object should support. class CashRegister { public: void clear(); void add_item(double price); double get_total() const; int get_count() const; private: // data members will go here }; Notice that these are just declarations. They will be defined later.

You call the member functions by first creating a variable of type CashRegister and then using the dot notation: Because these are mutators, the data members in the object will be changed. CashRegister register1;... register1.clear();... register1.add_item(1.95);

Every CashRegister object has its own copy of the data members

The Philosophy of Private Data Members More “secure” programs – not “abused” by clients. Example: if we use private, we can write a mutator for item_count so that item_count cannot be set to a negative value. If item_count were public, it could be directly set to a negative value by some misguided (or worse, devious) programmer.

The interface for our class:

Implementing the Member Functions void add_item(double price) { item_count++; total_price = total_price + price; } The details of the add_item member as a regular function: However, to specify that a function is a member function of your class you must write CashRegister:: in front of the member function’s name:

void CashRegister::add_item(double price) { item_count++; total_price = total_price + price; }

Implementing the Member Functions Use CashRegister:: only when defining the function – not in the class definition. class CashRegister { public:... private:... }; void CashRegister::add_item(double price) { item_count++; total_price = total_price + price; } Not here Only here Function definition

Often, separate the class definition from the implementation - Put in separate files - Class definition in a header file with.h extension - Implementation in a source file with a.cpp extension - In main(), have #include “ClassName.h”

Constructors class CashRegister { public: CashRegister(); // A constructor... }; The name of a constructor is identical to the name of its class

Constructors There must be no return type, not even void. class CashRegister { public: CashRegister(); // A constructor... };

Constructors And, of course, you must define the constructor. CashRegister::CashRegister() { item_count = 0; total_price = 0; }

Constructors Constructors can have parameters, and constructors can be overloaded: class BankAccount { public: // Sets balance to 0 BankAccount(); // Sets balance to initial_balance BankAccount(double initial_balance); // Member functions omitted private: double balance; };

Constructors BankAccount Ivans_account; // Uses default BankAccount constructor BankAccount Elenas_account(499.95); // Uses BankAccount(double) constructor

Inheritance - Implementing Derived Classes The : symbol denotes inheritance. class ChildClass : public ParentClass { public: // New and changed member // functions will go here private: // Additional data members // will go here }; Derived class Parent: the base class

The derived class inherits all data members and all functions of the base class that it does not override. Override by re-defining data members and member functions in derived class.

Function Overloading and Polymorphism Having functions with the same name is called function overloading Polymorphism is what allows functions with the same name to do different things based on its arguments. Polymorphism is available for objects.