Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Similar documents
C++ Classes, Constructor & Object Oriented Programming

Object-Oriented Programming, Classes

Object-Oriented Programming Concepts

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Lecture 7. Log into Linux New documents posted to course webpage

Object Oriented Programming COP3330 / CGS5409

Pointers, Arrays and C-Strings

Object Oriented Design

Data Structures using OOP C++ Lecture 3

Introduction to Classes and Objects How to manage data and actions together. Slides #10: Chapter

Introduction Of Classes ( OOPS )

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory.

Important when developing large programs. easier to write, understand, modify, and debug. each module understood individually

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Inheritance, and Polymorphism.

Constructor - example

OBJECT ORIENTED PROGRAMMING

Module 7 b. -Namespaces -Exceptions handling

Interview Questions of C++

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

A A B U n i v e r s i t y

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Object-Oriented Programming

Fast Introduction to Object Oriented Programming and C++

Introduction to Classes

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.


Implementing an ADT with a Class

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

EL2310 Scientific Programming

Programming in the Large II: Objects and Classes (Part 1)

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

CS304 Object Oriented Programming Final Term

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Recharge (int, int, int); //constructor declared void disply();

Cpt S 122 Data Structures. Introduction to C++ Part II

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

What is Class? Remember

C++ Programming Classes. Michael Griffiths Corporate Information and Computing Services The University of Sheffield

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

CSCE 110 PROGRAMMING FUNDAMENTALS

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

JAVA: A Primer. By: Amrita Rajagopal

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Chapter 14 Abstract Classes and Interfaces

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

OBJECT ORIENTED PROGRAMMING

UFCE3T-15-M Object-oriented Design and Programming

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

How to engineer a class to separate its interface from its implementation and encourage reuse.

2. It is possible for a structure variable to be a member of another structure variable.

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Computer Programming C++ Classes and Objects 6 th Lecture

Abstraction in Software Development

Intermediate Programming, Spring 2017*

Appendix G: Writing Managed C++ Code for the.net Framework

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

University of Swaziland Department OfComputer Science Main Examination December 2016

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

G52CPP C++ Programming Lecture 13

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

G52CPP C++ Programming Lecture 9

Function Overloading

Chapter 8 Objects and Classes

G52CPP C++ Programming Lecture 14. Dr Jason Atkin


Friends and Overloaded Operators

Introduction to Object-Oriented Programming with C++

Friends and Overloaded Operators

AN OVERVIEW OF C++ 1

Ch02. True/False Indicate whether the statement is true or false.

Chapter 1: Object-Oriented Programming Using C++

EL2310 Scientific Programming

SFU CMPT Topic: Classes

Class Destructors constant member functions

ECOM 2324 COMPUTER PROGRAMMING II

Object-Oriented Programming for Scientific Computing

Object Oriented Design

CSC1322 Object-Oriented Programming Concepts

Object Oriented Programming

Object Oriented Programming in C#


EL2310 Scientific Programming

CS24 Week 3 Lecture 1

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

CMSC 202 Midterm Exam 1 Fall 2015

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

ADTs in C++ In C++, member functions can be defined as part of a struct

CPSC 427: Object-Oriented Programming

lecture04: Constructors and Destructors

Transcription:

Classes - 2 Data Processing Course, I. Hrivnacova, IPN Orsay OOP, Classes Reminder Requirements for a Class Class Development Constructor Access Control Modifiers Getters, Setters Keyword this const Member Functions Destructor 1

Object-Oriented Programming (OOP) The basic unit of OOP is a class A class is an implementation of an abstract data type: Chua Hock-Chuan: Programming Notes It describes both the attributes (data) of an object and its operations (methods) The OOP languages let you think in the problem space, and use software objects to represent and abstract entities of the problem space to solve the problem. 2

A Class A class can be visualized as a three-compartment box, as illustrated: Classname (or identifier): identifies the class. Data Members (or variables, attributes, states, fields): Member Functions (or methods, behaviors, operations): Chua Hock-Chuan: Programming Notes contain the static attributes of the class. contain the dynamic operations of the class. In other words, a class encapsulates the static attributes (data) and dynamic behaviors (operations that operate on the data) in a box. Class Members: The data members and member functions are collectively called class members. 3

Example of a Class, Instances A class is a definition of objects of the same kind. Circle An instance is a realization of a particular item of a class. c1, c2, c3 Chua Hock-Chuan: Programming Notes 4

Requirements for a Class Before a class is implemented, two issues must be clarified: What should we be able to do with objects of a class? Which data represent objects of this class? Requirements for the Circle class: It represents a circle of a given size and a color We can get its size (radius, area) and its color Data contained: radius (the area can be computed), color 5

// The Circle class (All source codes in one file) #include <iostream> // using IO functions #include <string> // using string using namespace std; class Circle { private: double radius; string color; // Data member (Variable) // Data member (Variable) Class Circle Data members public: // Constructor with default values for data members Circle(double r = 1.0, string c = "red") { radius = r; color = c; double getradius() { return radius; // Member function (Getter) string getcolor() { return color; // Member function (Getter) Member functions double getarea() { // Member function return radius*radius*3.1416; ; // need to end the classi. Hrivnacova declaration with a semi-colon @ Data Processing Course 2018 6

Using Class Circle // 'testcircle' main function int main() { // Construct an instance of Circle c1 Circle c1(1.2, "red"); cout << "Radius=" << c1.getradius() << " Area=" << c1.getarea() << " Color=" << c1.getcolor() << endl; c1.setradius(2.1); // Change radius and color of c1 c1.setcolor("blue"); cout << "Radius=" << c1.getradius() << " Area=" << c1.getarea() << " Color=" << c1.getcolor() << endl; // Construct another instance using the default constructor Circle c2; cout << "Radius=" << c2.getradius() << " Area=" << c2.getarea() << " Color=" << c2.getcolor() << endl; 7

Using Class Circle // 'testcircle' main function int main() { // Construct an instance of Circle c1 Circle c1(1.2, "red"); cout << "Radius=" << c1.getradius() << " Area=" << c1.getarea() << " Color=" << c1.getcolor() << endl; c1.setradius(2.1); // Change radius and color of c1 c1.setcolor("blue"); cout << "Radius=" << c1.getradius() << " Area=" << c1.getarea() << " Color=" << c1.getcolor() << endl; Program // Construct another instance using the defaultoutput: constructor >./testcircle Circle c2; cout << "Radius=" << c2.getradius() <<Radius=1.2 " Area=" << c2.getarea() Area=4.5239 Color=blue << " Color=" << c2.getcolor() <<Radius=3.4 endl; Area=36.3169 Color=red Radius=1 Area=3.1416 Color=red 8

Constructor // Constructor with default // values for data members Circle(double r = 1.0, std::string c = "red") { radius = r; color = c; A constructor is a special member function that is used to construct the object A constructor function is different from an ordinary function in the following aspects: The name of the constructor is the same as the class name. Constructor has no return type. Hence, no return statement is allowed inside the constructor's body. Constructor can only be invoked once to initialize the instance constructed. // Construct a Circle instance Circle c1(1.2, "blue"); Circle c2(3.4); // default color Circle c3; // default radius and color // Take note that there is // no empty bracket () A class may have several constructors 9

Initialization List // Constructor with // values assignment Circle(double r = 1.0, std::string c = "red") { radius = r; color = c; // Constructor with // initialization list Circle(double r = 1.0, string c = "red") : radius(r), color(c) { The data members values can be set using so called initialization list It includes all the members of the class, following the same order as their declaration in the class definition It is not mandatory, but if it is not done, it is done by the compiler using random values 10

Access Control Modifiers Used to control the visibility of a data member or a member function within a class Data encapsulation (or information hiding): Objects communicate with each others using well-defined interfaces (public functions). Objects are not allowed to know the implementation details of others. The implementation details are hidden or encapsulated within the class. Information hiding facilitates reuse of the class. public: the member is accessible and available to all in the system. private: The member is accessible and available within this class only. You cannot use "c1.radius" to refer to c1's radius in main(): Try inserting the statement "cout << c1.radius;" in main() and observe the error message: error: 'radius' is a private member of 'Circle' The getradius() function is declared public, hence, it can be invoked in the main() Rule of Thumb: Do not make any data member public, unless you have a good reason. 11

Class Getters // Class getter double getradius() { return radius; To allow other to read the value of a private data member, eg. radius in our class Circle, you shall provide a get function, eg. getradius() // Using getter in main() Circle c1(1.2, "blue"); cout << "Radius=" << c1.getradius << endl; A getter need not expose the data in raw format. It can process the data and limit the view of the data others will see. Getters shall not modify the data member. 12

Class Setters // Class setter void setcolor(string c) { color = c; // Using setter in main() Circle c1(1.2, "blue"); c1.setcolor("red"); There is no way you can change the radius or color of a Circle object, after it is constructed in main(), as they are declared as private To allow other classes to modify the value of a private data member, eg. color in our class Circle, you shall provide a set function, eg. setcolor() A setter could provide data validation (such as range checking), and transform the raw data into the internal representation. 13

Keyword "this" class Circle { private: string color; // Member variable // called "color"... public: void setcolor(string color) { // Function's argument also called "color" this->color = color; // "this.color" refers to this instance's // member variable // "color" refers to the function's argument.... One can use keyword "this" to refer to this instance inside a class definition. It can be used when we need to refer to the class object. "this" is actually a pointer to the object of the class. 14

Data Members Naming class Circle { private: string mcolor; // Member variable... public: void setcolor(string color) { mcolor = color;... Using an agreed prefix, eg. m, with the data member names Makes data members "visible" withing the class implementation Avoids the ambiguity between the names of data member and function parameter. 15

"const" Member Functions Member function declared with a const keyword cannot modify any data member of this object. The getters can be always declared constant. double getradius() const { // const member function radius = 0; // error: cannot assign to non-static data member // within const member function 'getradius' return radius; double getarea() const { // const member function return radius*radius*3.1416; 16

Destructor class Circle { public:... // destructor ~Circle() {... ; A destructor is a special function that has the same name as the classname, with a prefix ~, e.g., ~Circle(). Destructor is called implicitly when an object is destroyed. If you do not define a destructor, the compiler provides a default, which does nothing. If your class contains data members which are dynamically allocated (via new or new[] operator), you need to free the storage via delete or delete[]. 17

Separating Header and Implementation We shall "separate the interface and implementation" by placing the codes in 3 files: Circle.h: defines the public interface of the Circle class. Circle.cxx: provides the implementation of the Circle class. testcircle.cxx: a test driver program for the Circle class. 18

#include <string> using namespace std; Circle.h // Circle class declaration class Circle { public: // Constructor with default values Circle(double radius = 1.0, string color = "red"); // Public getters & setters for private data members double getradius() const; void setradius(double radius); The header file contains string getcolor() const; declaration statements, that tell the void setcolor(string color); // Public member Function double getarea() const; compiler about the names and types, and function prototypes without the implementation details. The parameter names in function private: prototypes are ignored by compiler, // private data members (variables) but good to serve as documentation double mradius; string mcolor; ; 19

// user-defined header in the same directory #include "Circle.h" // Constructor // default values cannot be repeated in definition Circle::Circle(double radius, string color) : mradius(radius), mcolor(color) { // Public getter for private data member mradius double Circle::getRadius() const { return mradius; // Public setter for private data member mradius void Circle::setRadius(double r) { mradius = r;... Circle.cxx The implementation file provides the definition of the functions Must #include "Circle.h" You need to include the classname:: (called class scope resolution operator) in front of all the members names // A public member function double Circle::getArea() const { return mradius*mradius*3.14159265; So as to inform the compiler this member belong to a particular class. 20

#include <iostream> #include "Circle.h" using namespace std; // using Circle class testcircle.cxx // 'testcircle' main function int main() { // Construct an instance of Circle c1 Circle c1(1.2, "red"); cout << "Radius=" << c1.getradius() << " Area=" << c1.getarea() << " Color=" << c1.getcolor() << endl; c1.setradius(2.1); // Change radius and color of c1 c1.setcolor("blue"); cout << "Radius=" << c1.getradius() << " Area=" << c1.getarea() << " Color=" << c1.getcolor() << endl; // Construct another instance using the default constructor Circle c2; cout << "Radius=" << c2.getradius() << " Area=" << c2.getarea() << " Color=" << c2.getcolor() << endl; 21