1 C++ Programming Classes ● Class definition and implementation ● Constructors, data members ● Member Functions ● Structuring C + + code.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Plab 2003 Lecture 41 C++ - Classes. Plab 2003 Lecture 42 Motivating Example Goal: Graphics package Handle drawing of different shapes Maintain list of.
1 CS 201 Introduction to c++ (2) Debzani Deb. 2 Classes (1) A class definition – in a header file :.h file A class implementation – in a.cc,.cpp file.
Road Map Introduction to object oriented programming. Classes
. 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; };
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 CSE 303 Lecture 21 Classes and Objects in C++ slides created by Marty Stepp
1 Lecture Note 9_Polymorphism Outline Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class Functions from Derived-Class object Virtual.
Abstract Data Types and Encapsulation Concepts
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.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
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.
Object-Oriented Programming: Polymorphism Zhen Jiang West Chester University
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Object Oriented Programming (OOP) Lecture No. 10.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
1 CSC241: Object Oriented Programming Lecture No 02.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
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.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Xiaoyan Li CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 2 ADT and C++ Classes (I) Instructor: Zhigang Zhu Department of Computer Science City.
Classes, Interfaces and Packages
1 Classes: A class is a concept of something Vehicle 4 wheels, seats, engine, windows, color…… accelerate, start, stop, turn, lock, Truck.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
CS 116 Object Oriented Programming II Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Java Methods and Applications CSIS 3701: Advanced Object Oriented Programming.
The C++ Program Structure
Classes C++ representation of an object
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.
C++ Programming Functions
Chapter 9 - Object-Oriented Programming: Inheritance
CSC212 Data Structure - Section BC
A First C++ Class – a Circle
C++ Programming Functions
Classes Object-oriented programming: Example: Bank transactions
Review: Two Programming Paradigms
Designing Classes Eric Roberts CS 106B January 16, 2015.
Object Oriented Programming
IFS410: Advanced Analysis and Design
Week 6 Object-Oriented Programming (2): Polymorphism
Chapter 9 Objects and Classes
Learning Objectives Classes Constructors Principles of OOP
Object-Oriented Programming: Polymorphism
Chapter 9 - Object-Oriented Programming: Inheritance
Virtual Functions Example
Classes C++ representation of an object
Dr. R Z Khan Handout-3 Classes
Object oriented programming (OOP) Lecture No. 6
Separating Interface from Implementation
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

1 C++ Programming Classes ● Class definition and implementation ● Constructors, data members ● Member Functions ● Structuring C + + code

2 Introduction ● We will demonstrate the development of C++ code on a simple class representing a point in 2D space ● We will start from the requirements that our class should fulfil: – Point can be created from its x,y coordinates – It can calculate the distance to another point – It can return its coordinates x and y ● Class definition and implementation – In the class definition, we provide only the declaration of member functions and data members ● Like a function declaration, the class definition is a contract which defines the functions the class must take – The class member functions implementations (definitions) are usually provided in a separate file, called also the class implementation

3 Class Definition Class definition: class class-name { class-body }; The body of the class (class-body) consists of declarations of member functions and data members of the class: – The functions used to manipulate the object from outside are in the "public" area – The members are in the "private" area (Reminder: encapsulation) – In the "private" area one can also find functions that are accessible only by the class itself class class-name { public: // Function used to construct the object // Function to handle the object // Function to access the state of the object private: // Data members };

4 The definition of class Point class Point { public: // Functions used to construct the object Point(float x, float y); Point(); // Functions to handle the object float Distance(Point point); // Functions to access the state of the object float GetX(); float GetY(); private: // Data members float m_x; float m_y; }; class class-name { public: // Functions used to construct the object // Functions to handle the object // Functions to access the state of the object private: // Data members }; ● The choice of member functions is driven by the requirements: – Point can be created from its x,y coordinates – It can calculate the distance to another point – It can return its coordinates x and y File Point.h

5 Class Implementation Member Functions The definition of member functions to manipulate the object: return-type class-name::function-name { function-body } The functions of the class Point: float Point::Distance(Point point) { /// Calculate the distance to a point float x_diff = m_x – point.m_x; float y_diff = m_y – point.m_y; return sqrt(x_diff*x_diff + y_diff*y_diff); } float Point::GetX() { return m_x; } float Point::GetY() { return m_y; } In file Point.cxx float Distance(Point point) Point(); float GetX(); float GetY() ; In file Point.h

6 Constructors The constructor of the class Point from coordinates x, y: Point::Point(float x, float y) : m_x(x), m_y(y) { /// The constructor of the class Point from /// coordinates x, y } class-name::class-name(arguments-list) : data-member-initialisation { function-body } ● The constructor is a special member function that is used to construct the object ● The initialization list (data-member- initialization) 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 ● A class may have several constructors In file Point.cxx Point(float x, float y); In file Point.h

7 The Default Constructor The default constructor is a special constructor which has no arguments: The default constructor of the class Point: class-name::class-name() : initialisation-list { function-body } Point::Point() : m_x(0), m_y(0) { /// The default constructor /// (The point is initialized to the center) } – The data members are initialized to their default values In file Point.cxx Point(); In file Point.h

8 Using classes #include “Point.h” int main() { // Create two points Point point1; Point point2(2, 3); // Calculate and print their distance std::cout << point1.Distance(point2) << std::endl;... } ● The following code can appear in the main() function of the program or in another class In file main.cxx

9 C++ Code Structuring... ● The code is usually structured by classes ● The definition of the class is separated from the implementation of its methods, so we have two files per class – Header file – extension.h (.hh) – Implementation file – extension:.cxx (.cpp,.cc,.C,...) ● The header file is sometimes called the class interface, what should not be confused with the same term used for abstract classes class Point { public: Point(float x, float y); float Distance(Point point); private: float m_x; float m_y; }; #include “Point.h” Point::Point(float x, float y) : m_x(x), m_y(y) {} float Point::Distance(Point point) { float x_diff = m_x – point.m_x; float y_diff = m_y – point.m_y; return sqrt(x_diff*x_diff + (x_diff*x_diff); } #include “Point.h” #include int main() { Point p1(0, 0); Point p2(2, 3); std::cout << p1.Distance(p2) << std::endl; return 0; } Point. h Point.cx x main.cx x

10... C++ Code Structuring ● Classes can be structured in packages, the files are then placed in individual directories per package – Often the directory is divided into 2 sub-directories: include (*.h), src (*.cxx) ● The design must deal with relationships (dependencies) between packages – We must define the package hierarchy – Avoid circular dependencies ● The coding style can vary from one project to another but the practice is to adopt and to follow it consistently: – Place the public space before the private – Data members begin with m_ – Function members begin with a capital letter –...