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.

Slides:



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

What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
Road Map Introduction to object oriented programming. Classes
Chapter 14: Overloading and Templates
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Review of C++ Programming Part II Sheng-Fang Huang.
 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.
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.
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.
Learners Support Publications Classes and Objects.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ECE122 Feb. 22, Any question on Vehicle sample code?
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
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.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
1 CSC241: Object Oriented Programming Lecture No 02.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CONSTRUCTOR AND DESTRUCTORS
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Learning Objectives Pointers as dada members
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.
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
Chapter 5 Classes.
Constructor & Destructor
C++ Classes & Object Oriented Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 9 Classes: A Deeper Look, Part 1
Learning Objectives Classes Constructors Principles of OOP
Classes and Objects.
Java Programming Language
Classes: A Deeper Look, Part 1
Classes C++ representation of an object
Constructors & Destructors
SPL – PS3 C++ Classes.
Presentation transcript:

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 only data, it can hold both data and functions. It is just a collection of variables with a set of related functions. The variables in the class are referred to as the member variables or data members. The functions in the class manipulate the member variables. They are referred to as member functions or methods of the class. 2

An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. A class enables you to encapsulate these variables and functions into one collection, which is called an object. Declaring a Class To declare a class, use the class keyword followed by a class name and an opening brace, then list the data members and methods of that class. End the declaration with a closing brace and a semicolon. 3

Example: class Part { int modelnumber; double cost; void SetPart(int mn, double c); void ShowPart(); }; Note : You cannot initialize data members where you declare them. A method declaration can also include the implementation, or you can implement it separately). All methods can be accessed only through an object of the class. 4

Defining an object An object is an individual instance of a class. To define an object of a new type just as defining an integer variable (or any other variable): Part wheel; This code defines wheel, which is an object whose class (or type) is Part. 5

Calling Data Member and methods Once you define an actual Part object, for example, wheel you use the dot operator (.) to access the members of that object. Therefore, to assign 50 to wheel’s modelnumber member variable wheel.modelnumber = 50; In the same way, to call the ShowPart() function, wheel.ShowPart(); //method calling 6

Assign Values to Objects, Not to Classes In C++ you don't assign values to types; you assign values to variables. For example, int x = 50;//define x to be an int int = 50; // wrong In the same way, Part.modelnumber = 50; //wrong you must define a Part object and assign 50 to the modelnumber of that object. Part wheel; //just like int x; wheel.modelnumber = 50; //just like x = 50; 7

Using Access Specifiers C++ allows to control where the data members of a class can be accessed to protect them. An access specifier is a word that controls where the data members in a class can be accessed. An access specifier affects all members of the class that come after it until another access specifier is encountered or until you reach the end of the class. class ClassName { classMembers; accessSpecifier: classMembers;}; 8

A class has two kinds of access specifiers: public and private. public members can be accessed anywhere that an object of the class can be accessed and from within the class (that is, in the class’s methods). private members can be accessed only from within the class itself. Note: an object of the class cannot access the private members, except through public methods. If no access specifier is provided in the class, all members default to private. 9

Example class Part { public: int modelnumber; double cost; void SetPart(int m, double c); void ShowPart(); }; Now wheel.modelnumber = 50; compiles without problems. 10

Private and Public Data Usually the data within a class is private and the functions are public. However, there is no rule for that. 11

12

Memory Allocation Declaring this class doesn't allocate memory for a Part. It just tells the compiler what a Part is, what data it contains, and what it can do. It also tells the compiler how big a Part is (that is, how much room the compiler set for each Part (objects) that you create). 13

Creating Methods You can declare a method two ways. 1. The most common way is to declare a method inside the class declaration and then implement it outside. 2. The second way is to declare and implement the method at the same time inside the class declaration. However, there is special syntax for the method definition. A member function definition begins with the return type, followed by the name of the class, two colons (::), the name of the function, and its parameters. 14

The general syntax for a method implementation that occurs outside a class: return_type ClassName::methodName(parameterList) { method implementation; } The double colon (::) is called the scope resolution operator. 15

Constructor and Destructor These are special kinds of methods that can be in a class, and they are optional They provide special functionality that other methods cannot provide. A constructor is executed every time you declare a new object. It is normally used to set initial values for the data members. always has the same name as the class and cannot have a return value (not even void). 16

A destructor is the opposite of a constructor and is executed when the object is destroyed. It is always named the same name as the class, but with a tilde (~) at the beginning. It cannot have arguments or a return value. A destructor is often used to perform any necessary cleanup tasks. 17

Both constructors and destructors are like methods; they can be declared and implemented at the same time or declared and implemented separately. The syntax for declaring and implementing at the same time: class ClassName {//constructor ClassName(argumentList) {implementation;} //destructor ~ClassName() {implementation;} //other members }; 18

Here is the syntax for declaring and then implementing: class ClassName { ClassName(argumentList); ~ClassName(); //other Member; }; ClassName::ClassName([argumentList]) {implementation;} ClassName::~ClassName() {implementation;} 19

Notice : the constructor can have arguments. If you create a constructor with arguments, the user of the class must supply values for these arguments when creating an object. Notice : the destructor cannot have arguments. It is called automatically, so no chance for the user to provide arguments. Because a constructor can have arguments, it might become necessary to overload the constructor. This is legal in C++ and is quite common in large classes. Overloading the constructor in this way gives your class versatility and provides users of the class with many options. The destructor cannot be overloaded since there is no return type or arguments. 20

21

22

23

24

25

26

Separating Classes into Files What are the benefit of separating classes in to files ? Normally, to do the separation, the class declaration is placed in one file (header file), and the implementation of all the methods is put in another file. The class declaration file is normally called ClassName.h. The implementation file is normally called ClassName.cpp. Then you include the header file in your program with an #include directive. 27

However, instead of including two files (ClassName.h and ClassName.cpp), you have to include only ClassName.h. The compiler will include the.cpp file automatically Don’t forget: the two files need to be in the same directory to achieve that). 28

29

30

Pointers To Objects An object of a class has a memory address. You can assign this address to a suitable pointer. For example Part part1(320); Part * partPtr = &part1; Now we can use the pointer like : (*partPtr).setPart(); 31

Arrow Operator You can use the class member access operator -> (arrow operator) instead of a combination of (*) and (.). Syntax: objectPointer->member This expression is equivalent to (*objectPointer).member The difference between the class member access operators (.) and (->) is that the left operand of the dot operator must be an object, whereas the left operand of the arrow operator must be a pointer to an object. 32

Any Question? Refer to chapter 1 of the book for further reading 33