Lecture #6 Classes and Objects.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
1 Chapter 11 Introducing the Class Pages ( )
C++ Classes & Data Abstraction
Chapter 12: Classes and Data Abstraction
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
What is an object? Your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior;
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
1 Writing a Good Program 5. Objects and Classes in C++
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CSC241: Object Oriented Programming
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
PART I CHAPTER 16 CS116 SENEM KUMOVA METİN 1. Structures Structures : Aggregate data types built using elements of other types struct Time { int hour;
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Object-Oriented Programming in C++
Structured Programming Instructor: Prof. K. T. Tsang Lecture 13:Object 物件 Oriented 面向 Programming (OOP)
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
C++ Review (3) Structs, Classes, Data Abstraction.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Object-Oriented Programming Using C++ CLASS 1. 2 Review of Syllabus Catalog Description –An introduction to object oriented programming techniques using.
Chapter 10 Introduction to Classes
CS1201: Programming Language 2 Classes and objects By: Nouf Aljaffan Edited by : Nouf Almunyif.
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.
 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.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
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.
Lecture 06 Java and OOP Jaeki Song. Outlines Java and OOP –Class and Object – Inheritance – Polymorphism.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
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.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 03.
CS1201: Programming Language 2 Classes and objects.
1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.
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:
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
CIS162AD Constructors Part 2 08_constructors.ppt.
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.
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.
Comp1004: Building Better Objects II Encapsulation and Constructors.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Introduction to Object-oriented Programming
Procedural and Object-Oriented Programming
About the Presentations
Object Lifetime and Dynamic Objects
CS1201: Programming Language 2
group work #hifiTeam
CS1201: Programming Language 2
Object Oriented Analysis and Design
Programming -2 برمجة -2 المحاضرة-7 Lecture-7.
CLASSES AND OBJECTS.
Introduction to Classes and Objects
CS1201: Programming Language 2
NAME 436.
Types of Computer Languages
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Presentation transcript:

Lecture #6 Classes and Objects

Objects You can look around you now and see many examples of real-world objects: your cat, your desk, your television set, your bicycle. These real-world objects share two characteristics: they all have state and they all have behavior For example, dogs have state (name, color, breed, hungry) and dogs have behavior (barking, fetching).

Objects Software objects are modeled after real-world objects in that they, too, have state and behavior. A software object maintains its state in variables and implements its behavior with methods. An object combines data and operations on the data into a single unit.

Classes In OOD, the first step is to identify the components, called objects. An object combines data and operations on the data into a single unit. In C++, the mechanism that allow you to combine data and operations on the data into a single unit is called a class. A class is a collection of fixed number of components. The components of a class are called the members of the class.

CLASSES class definition: classMembersList consists of variables (attribute) and/or functions (method) declarations . class classIdentifier { classMembersList };

Classes

Classes Members of a class are classified into one of three categories: Private (default) : not accessible outside the class. Protected: not accessible outside the class. Public: accessible outside the class. Note: The keyword of category name is followed by colon (:) . Usually the data within a class is private and the functions are public. In the definition of a class, you cannot initialize a variable when you declare it. In C++, a class is a definition. No memory is allocated for the class itself; memory is allocated for the class objects (class instances) when you declare them.

Example int main( )‏ { Person student; student.setAge(20); student.setName(“Nora”); cout << ”student\'s age is " << student.getAge(); cout << " and Name " << student.getName() << ” \n“ ; cout << endl; return 0; } #include <iostream> using namespace std; Class Person { private: int age; string name ; public: void setAge (int yrs) { age = yrs; } void setName (string N) { name = N; } int getAge() { return age; } string getName() { return N; } };

Classes Defining Objects: Calling Member Functions: Person student; student.setAge(20); student.setName(“Nora”);

Constructors Vs. Destructor Constructors guarantee that the member variables are initialized when an object is declared. Constructors automatically execute when a class object is declared. The name of a constructor is the same as the name of the class. A class can have more than one constructor, Constructor Overloading. A constructor without parameters is called the default constructor Destructor automatically execute when a class object goes out of scope. The name of a destructor is the tilde (~), followed by the class name (no spaces in between). A class can have only one destructor. The destructor has no parameters.

Constructors & Destructor Constructors and Destructor are functions without any type. As a result, they cannot be called like other functions. Note : A function can return a value of type class.

Example # 1 Circle::Circle(float r)‏ { setRadius(r); } // destructor Circle::~Circle()‏ {cout<<" ending object.."<<endl; void Circle::setRadius(float r)‏ { if ( r >=0)‏ radius=r; else radius=0; float Circle::getRadius()‏ { return radius; float Circle::area()‏ return 3.14*radius*radius; int main()‏ { Circle C1(5); cout<<"\n the area of the circle is: "<<C1.area()<<end l; return 0; } #include<iostream> using namespace std; class Circle { private: float radius; public: Circle(); Circle(float r); ~Circle(); void setRadiu (float r); float getRadius(); float area(); }; // constructors Circle::Circle()‏ { radius=0; }

Member Functions Defined Outside the Class void Circle::setRadius(float r) The function name, setRadius(), is preceded by the class name, Circle, and a new symbol the double colon :: the scope resolution operator. It is a way of saying, Circle::setRadius() means “the setRadius() is a member function of the Circle class.” ‏

Example #2

Example#3 class country { private: int num_of_cities; public: country() {cout<<"\n Constructor called \n"; } void setNumOfCities(int num); int getNumOfCities(void); ~country() { cout<<"\n Destructor called \n"; } };

Con. Example#3 void country::setNumOfCities(int num) { num_of_cities = num; } int country::getNumOfCities(void) { return num_of_cities; } int main(void) { country Kuwait; int num = 5; Kuwait.setNumOfCities(num); num = Kuwait.getNumOfCities(); cout<<"\n Number of cities is equal to "<<num; return 0; }

Example #4 int main() { Book ABook(128) ; ABook.SetPage( 56 ); Cout << “The current page is: “ << ABook.GetCurrentPage() ; Return 0; } #include <iostream> #include <stdio.h> class Book { int PageCount; int CurrentPage; public: Book( int Numpages) ; // Constructor ~Book(){} ; // Destructor void SetPage( int PageNumber) ; int GetCurrentPage( void ) ; }; Book::Book( int NumPages) { PageCount = NumPages; } void Book::SetPage( int PageNumber) { CurrentPage=PageNumber; } int Book::GetCurrentPage( void ) { return CurrentPage; }