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

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
Object Oriented Programming and Object Oriented Design Programming Languages Robert Dewar.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Abstract data types & object-oriented paradigm. Abstraction Abstraction: a view of an entity that includes only the attributes of significance in a particular.
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.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Programming Languages and Paradigms Object-Oriented Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Chapter 9 Classes: A Deeper Look, Part I Part II.
Object-Oriented Programming Chapter Chapter
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
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
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:
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Constructors and Destructors
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Programming with ANSI C ++
Inheritance and Run time Polymorphism
Review: Two Programming Paradigms
CS410 – Software Engineering Lecture #11: C++ Basics IV
Introduction to Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Class: Special Topics Copy Constructors Static members Friends this
Pointers Revisited What is variable address, name, value?
CS212: Object Oriented Analysis and Design
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Introduction to Classes
CS212: Object Oriented Analysis and Design
Constructors and destructors
Constructors and Destructors
CS410 – Software Engineering Lecture #5: C++ Basics III
CS148 Introduction to Programming II
SPL – PS3 C++ Classes.
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

Classes Encapsulation of type and related operations 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); 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 body: each method can be defined separately 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.

Declaration and definition Declaration: specify type and name –int x; –double foo(std::string name); Definition: everything else double MyClass::foo(std::string name) { return -1;} Can do both at once –int x = 3;

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.

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 (not one for each object) 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 ();

Static methods A method can be static: class Decoder { static bool decode(int x); } usage: Decoder::decode(93); * In C++ a class cannot be static

Classes and private types If all data members are private, class is identical to a private type (or ADT): visible methods, 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 (with new), need to reclaim (free) 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

Kinds of constructors Default MyClass() Parameterized MyClass(int x) Copy constructor MyClass(const MyClass& source) Conversion constructor Move constructor

Copy constructor stack (const stack& s) { // reference to existing object contents = new int [ sz = s.size()]; for (int I = 0; I <sz; I++) contents [I] = s.contents [I]; } 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 I = 0; I <sz; I++) contents [I] = s.contents [I]; } 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

Friends A class’s “friend” can access its private members. class Window { friend class Game; … private: int width_ }

& Safer than pointer –Cannot be null, so cannot be declared but not defined int& compilerUnhappy;// error

virtual functions Inheritable and overridable class Parent { virtual std::string talk(); } class Child : public Parent { std::string talk(); } Parent* adam = new Child(); adam->talk();

Visual Studio debug In DEBUG mode, Visual Studio “initializes” uninitialized heap memory with 0xCDCDCDCD. If you see this, you probably have a pointer you haven’t given a value to.