Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
CPS 506 Comparative Programming Languages Abstract Data Type and Encapsulation.
Road Map Introduction to object oriented programming. Classes
Object Oriented Programming and Object Oriented Design Programming Languages Robert Dewar.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
1 Object Oriented Programming. 2 What’s the Object? Global name space for functions in C Global name space for functions in C Every function is available.
Lecture 9 Concepts of Programming Languages
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Abstract Data Types and Encapsulation Concepts
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Chapter 11 Abstract Data Types and Encapsulation Concepts.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
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.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
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.
Copyright 2008 Oxford Consulting, Ltd 1 October C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Object-Oriented Programming Chapter Chapter
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.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
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.
ISBN Object-Oriented Programming Chapter Chapter
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Constructors and Destructors
Abstract Data Types and Encapsulation Concepts
Programming with ANSI C ++
CS410 – Software Engineering Lecture #11: C++ Basics IV
Chapter 3: Using Methods, Classes, and Objects
11.1 The Concept of Abstraction
Introduction to Classes
CS212: Object Oriented Analysis and Design
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Introduction to Classes
Abstract Data Types and Encapsulation Concepts
CS212: Object Oriented Analysis and Design
Constructors and destructors
Constructors and Destructors
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
CS410 – Software Engineering Lecture #5: C++ Basics III
Corresponds with Chapter 5
SPL – PS3 C++ Classes.
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management multiple inheritance

Ancestry Basis is C language: low-level constructs for systems programming, compact syntax Major innovation: classes, inspired by Simula. ◦ In Simula classes are objects with state and independent thread of execution. Syntactic innovation: operator overloading Later addition : templates Latest addition: a model of concurrency

Classes Encapsulation of type and related operations. Provides information hiding. Operations “belong” to the class class point { double x, y; // private data members public: point (int x0, int y0); // public methods point () { x = 0; y = 0;}; /// a constructor void move (int dx, int dy); // formal names optional void rotate (double alpha); int distance (point p); }

A class is a type : objects are instances point p1 (10, 20); // call constructor with given arguments point p2; // call default constructor Methods are functions with an implicit argument p1.move (1, -1); // special syntax to indicate object // in other languages might write move (p1, 1, -1) // special syntax inspired by message-passing metaphor: // objects are autonomous entities that exchange messages.

Implementing methods No equivalent of a package body: each method can be defined separately. Proper style is a separate file for all method bodies. void point::rotate (double alpha) { x = x * cos (alpha) - y * sin (alpha); y = y * cos (alpha) + x * cos (alpha); }; // x and y are the data members of the object on which the // method is being called. // if method is defined in class declaration, it is inlined.

Constructors One of the best innovations of C++ special method (s) invoked automatically when an object of the class is declared point (int x1, int x2); point (); point (double alpha; double r); point p1 (10,10), p2; p3 (pi / 4, 2.5); Name of method is name of class Declaration has no return type (would be redundant) Long form: point (int x1, int x2) {x = x1; y = x2} Syntactic sugar: point (int x1, int x2) :x(x1), y(x2)

The target of an operation The implicit parameter in a method call can be retrieved through this: class Collection { Collection& insert (thing x) { // return reference … modify data structure return *this; // to modified object }; my_collection.insert (x1).insert (x2);

Static members Need to have computable attributes for class itself, independent of specific object; e.g. number of objects created. Static qualifier indicates that entity is unique for the class static int num_objects = 0; point ( ) { num_objects++;}; // ditto for other constructors Can access static data using class name or object name: if (point.num_objects != p1.num_objects) error ( );

Classes and private types If all data members are private, class is identical to a private type: only methods are visible, including assignment. A struct is a class with all public members How much to reveal is up to programmer define functions to retrieve (not modify) private data int xcoord () { return x;}; int ycoord () { return y;}; p2.x = 15; // error, data member x is private

Destructors If constructor allocates dynamic storage, need to reclaim it class stack { int* contents; int sz; public: stack (int size) { contents = new int [ sz = size];}; void push ( ); int pop ( ); int size ( ) { return sz;}; } stack my_stack (100); // allocate storage dynamically // when is my_stack.contents released?

If constructor uses resources, class needs a destructor User cannot deallocate data because data member is private: system must do it ~stack ( ) {delete[ ] contents;}; inventive syntax: negation of constructor Called automatically when object goes out of scope Almost never called explicitly

Copy and assignment point p3 (10,20); point p5 = p3; // componentwise copy This can lead to unwanted sharing: stack stack1 (200); stack stack2 = stack1; // stack1.contents shared stack2.push (15); // stack1 is modified Need to redefine assignment and copy

Copy constructor stack (const stack& s) { // reference to existing object contents = new int [ sz = s.size( )]; for (int k = 0; k <sz; k++) contents [k] = s.contents [k]; } stack s1 (100); … stack s2 = s1; // invokes copy constructor

Redefining assignment assignment can also be redefined to avoid unwanted sharing operator returns a reference, so it can be used efficiently in chained assignments: one = two = three; stack & operator= (const stack& s) { if (this != &s) { // beware of self-assignment delete [ ] contents; // discard old value contents = new int [sz = s.size ( )]; for (int k = 0; k <sz; k++) contents [k] = s.contents [k]; } return *this; } stack s1 (100), s2 (200); … s1 = s2; // transfer contents

Anomalies An array whose component type is a class can only be declared if there is a parameterless constructor for the class. There is no way to pass parameters to the constructor. polygon point [10]; // ok turing stack [2]; // illegal

Operator Overloading: indexing Useful to create range-checked structures: class four_vect { double stor[4]; // private member, actual contents of vector. … double& operator[ ] (int j) { if (j 3) throw index_error; // defined elsewhere return stor[j]; }; Note: return type is a reference, so can be used on l.h.s

Extending the meaning of subscripting An associative array: class Assoc { // a map from strings to numbers struct Pair { // an inner class char* name; double val; Pair (char* n = “”, double v = 0) : name (n), val (v) { }; }; pair * contents; // Assoc is a set of pairs public: Assoc ( ) { }; // default constructor double& operator[ ] (const char *); // map string => number };