. Plab – Tirgul 6 Classes. Point.h class Point { public: Point(int x, int y); ~Point(); int getX() const; int getY() const; private: int m_x, m_y; };

Slides:



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

INHERITANCE BASICS Reusability is achieved by INHERITANCE
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Plab 2003 Lecture 41 C++ - Classes. Plab 2003 Lecture 42 Motivating Example Goal: Graphics package Handle drawing of different shapes Maintain list of.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
OOP Spring 2007 – Recitation 21 Object Oriented Programming Spring 2007 Recitation 2.
1 CSE 303 Lecture 21 Classes and Objects in C++ slides created by Marty Stepp
CS-2303 System Programming Concepts
Programming Languages and Paradigms Object-Oriented Programming.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
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.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
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.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
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 3 Introduction to Classes and Objects Definitions Examples.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Interfaces An interface is like an extreme case of an abstract class – However, an interface is not a class – It is a type that can be satisfied by any.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Introduction to Object Oriented Programming Chapter 10.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
1 C++ Programming Classes ● Class definition and implementation ● Constructors, data members ● Member Functions ● Structuring C + + code.
The C++ Program Structure
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Topic: Classes and Objects
Classes and Objects Introduced
Learning Objectives Pointers as dada members
Classes C++ representation of an object
Structs (C,C++) Already seen in tirgul.
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.
Chapter 9 More on Objects and Classes
Operator overloading Conversions friend inline
A First C++ Class – a Circle
Operator overloading Conversions friend inline
Introduction to Classes
Introduction to Classes
CLASS DEFINITION (FIELDS)
Simple Classes in Java CSCI 392 Classes – Part 1.
Basics of OOP A class is the blueprint of an object.
Chapter 9 - Object-Oriented Programming: Inheritance
Chapter 14 Abstract Classes and Interfaces
Class.
Object Oriented Programming
Chapter 11 Inheritance and Encapsulation and Polymorphism
Lecture 8 Object Oriented Programming (OOP)
SPL – PS3 C++ Classes.
Presentation transcript:

. Plab – Tirgul 6 Classes

Point.h class Point { public: Point(int x, int y); ~Point(); int getX() const; int getY() const; private: int m_x, m_y; };

#include "Point.h“ class Circle { public: Circle(int x, int y, double r); ~Circle(); //... private: Point m_center; //... }; Circle.h

#include "Circle.h“ Circle::Circle(int x, int y, double r) : m_center(x,y) { //... } Circle::~Circle() { printf(“in ~Circle()"); } Circle.cpp Won’t compile without

The same syntax also works for base types. Point::Point(int x, int y) : m_x(x), m_y(y) { } is better than Point::Point(int x, int y) { m_x = x; m_y = y; }

Inheritance u The main ideas are similar to what you already know from Java. u C++ has no interfaces but it allows multiple inheritance u For now we will not discuss the problems that arise: Class A Class CClass B Class D

#include "Point.h“ class Circle : Point { public: Circle(int x, int y, double r); ~Circle(); //... #include "Circle.h“ Circle::Circle(int x, int y, double r) : Point(x,y) { //... } Circle::~Circle() { printf(“in ~Circle()"); } private: //... }; Circle class - again Won’t compile without

Default arguments in constructors In C++ Point::Point(int x = 0, int y = 0) { //... } is equivalent to Java’s Point(int x, int y) {...} Point(int x) { this(x,0); } Point() {this(0,0);} Note that now Point has a default constructor! Nothing similar in C++

C-tor & D-tor order of execution u Constructor of the base class is the 1 st to be executed. u Then the members are constructed. u Finally, the constructor of the class itself is executed. u Destruction is done in the opposite order.

protected u Class members that should be accessible by subclasses are declared protected.  Example: to allow Circle access coordinates of Point we would define: class Point { public: //... protected: int m_x, m_y; };

this u A (constant) pointer to the instance for which the member function was invoked. u Example: class List { List* next; public: bool on(List*); //... }; bool List::on(List* p) { if (p==null) return false; for (List *q = this; q; q=q->next) if (q == p) return true; return false; }

inline functions u A hint to a compiler to put function’s code inline, rather than perform a regular function call. u Objective: improve performance of small, frequently used functions.  Example: allocateNode() function in data structures like Linked List or Binary Tree.  A member function defined in class definition (i.e. the header file) is automatically considered to be inline. u An inline function defined in.cpp file is not recognized in other source files.

Static class members u As in Java, these are members belonging to the class rather than to an instance. class X { private: static int instances; public: X() { instances++; } ~X(); { instances--; } static int getInstances() { return instances; } }; X.h

Static class members Example of using a static function: X x1; //... X x2; //... printf(“%d\n”, X::getInstances()); Somewhere (but only in one place!) the instances variable should be defined: int X::instances = 0; In Java declaration and defintion were in the same place