OOP objects and Classes Dr. Ahmed Hashim Mohammed A Simple Class

Slides:



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

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter 13 – Introduction to Classes
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ Classes & Data Abstraction
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
True or false A variable of type char can hold the value 301. ( F )
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Object Oriented Programming (OOP) Lecture No. 11.
Chapter 4 Introduction to Classes, Objects, Methods and strings
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.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 07 classes 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
1 Introduction to Object Oriented Programming Chapter 10.
Classes.  A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data.
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.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
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.
Introduction to Object Oriented Programming ( P ) Malik Jahan Khan 1.
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
Structures and Classes
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Classes (Part 1) Lecture 3
Overloaded Constructors and Multiple Classes
Topic Pre-processor cout To output a message.
Chapter 1.2 Introduction to C++ Programming
Class and Objects UNIT II.
C++ Templates.
Institute of Business & Technology (BIZTEK)
Classes.
Chapter 5 Classes.
Lecture 4-7 Classes and Objects
Chapter 4 Repetition Structures
Variables with Memory Diagram
Defining Classes and Methods
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Learning Objectives Classes Constructors Principles of OOP
Anatomy of a Function Part 1
Functions Pass By Value Pass by Reference
COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Classes and Objects.
Static in Classes CSCE 121 J. Michael Moore.
Lab 1 Introduction to C++.
Object Oriented Programming Using C++
Computing Fundamentals
CLASSES AND OBJECTS.
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Mr. Dave Clausen La Cañada High School
ECE 103 Engineering Programming Chapter 12 More C Statements
Defining Classes and Methods
Pointers & Functions.
Chapter 12: Classes and Data Abstraction
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Object Oriented Programming (OOP) Lecture No. 12
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

OOP objects and Classes Dr. Ahmed Hashim Mohammed A Simple Class Our first program contains a class and two objects of that class. Although it’s simple, the program demonstrates the syntax and general features of classes in C++. Here’s the listing for the SMALLOBJ program: #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// class smallobj //define a class { private: int somedata; //class data public: void setdata(int d) //member function to set data { somedata = d; } void showdata() //member function to display data { cout << “Data is “ << somedata << endl; } }; //////////////////////////////////////////////////////////////// int main() { smallobj s1, s2; //define two objects of class smallobj s1.setdata(1066); //call member function to set data s2.setdata(1776); s1.showdata(); //call member function to display data s2.showdata(); return 0; } The class smallobj defined in this program contains one data item and two member functions.The two member functions provide the only access to the data item from outside the class. Thefirst m ember function sets the data item to a value, and the second displays the value. Placing data and functions together into a single entity is a central idea in object-oriented programming. This is shown in Figure 6.1.

Classes and Objects Defining the Class OOP objects and Classes Dr. Ahmed Hashim Mohammed Classes and Objects Recall from Chapter 1 that an object has the same relationship to a class that a variable has to a data type. An object is said to be an instance of a class, in the same way my 1954 Chevrolet is an instance of a vehicle. In SMALLOBJ, the class—whose name is smallobj—is defined in the first part of the program. Later, in main(), we define two objects—s1 and s2—that are instances of that class. Each of the two objects is given a value, and each displays its value. Here’s the output of the program: Data is 1066 ← object s1 displayed this Data is 1776 ← object s2 displayed this We’ll begin by looking in detail at the first part of the program—the definition of the class smallobj. Later we’ll focus on what main() does with objects of this class. Defining the Class Here’s the definition (sometimes called a specifier) for the class smallobj, copied from the SMALLOBJ listing: class smallobj //define a class { private: int somedata; //class data public: void setdata(int d) //member function to set data

Private and Public Class Data Member Functions { somedata = d; } OOP objects and Classes Dr. Ahmed Hashim Mohammed { somedata = d; } void showdata() //member function to display data { cout << “\nData is “ << somedata; } }; The definition starts with the keyword class, followed by the class name—smallobj in this example. Like a structure, the body of the class is delimited by braces and terminated by a semicolon. Private and Public The body of the class contains two unfamiliar keywords: private and public. A key feature of object-oriented programming is data hiding. This term means that data is concealed within a class so that it cannot be accessed mistakenly by functions outside the class. The primary mechanism for hiding data is to put it in a class and make it private. Private data or functions can only be accessed from within the class. Public data or functions, on the other hand, are accessible from outside the class. This is shown in Figure 6.2. Class Data The smallobj class contains one data item: somedata, which is of type int. The data items within a class are called data members (or sometimes member data). There can be any number of data members in a class, just as there can be any number of data items in a structure. The data member somedata follows the keyword private, so it can be accessed from within the class, but not from outside. Member Functions Member functions are functions that are included within a class. There are two member functions in smallobj: setdata() and showdata(). The function bodies of these functions have been written on the same line as the braces that delimit them. You could also use the more traditional format for these function definitions: