Lecture 16 Oct 30, 02.

Slides:



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

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Chapter Objectives You should be able to describe: Assignment Pointers as Class Members Additional Class Features Common Programming Errors.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Definition Class In C++, the class is the construct primarily used to create objects.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
©2004 Brooks/Cole Chapter 10 More on Classes. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
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.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
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.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
1 CSC241: Object Oriented Programming Lecture No 03.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
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.
Learners Support Publications Constructors and Destructors.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Constructors and Destructors
Eine By: Avinash Reddy 09/29/2016.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Object-Oriented Programming Part 1 07_classes.ppt
Review What is an object? What is a class?
Chapter 9 Type Conversions
CONSTRUCTORS & DESTRUCTORS
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
group work #hifiTeam
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
Default Argument.
Contents Introduction to Constructor Characteristics of Constructor
Simple Classes in C# CSCI 293 September 12, 2005.
Learning Objectives Classes Constructors Principles of OOP
Object Oriented Programming
Constructors and Destructors
Object Oriented Programming (OOP) Lecture No. 13
Introduction of Programming
CS148 Introduction to Programming II
Method of Classes Chapter 7, page 155 Lecture /4/6.
CS 144 Advanced C++ Programming February 21 Class Meeting
Object-Oriented Programming (OOP) Lecture No. 23
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CS 201(Introduction To Programming)
CS148 Introduction to Programming II
Constructors & Destructors
CMSC 202 Constructors Version 9/10.
Introduction to Classes and Objects
Presentation transcript:

Lecture 16 Oct 30, 02

Recap Lec 15 We have a class. Class contains data and functions Access specifiers - private and public Objects are created from a class. Class forms the blueprint for objects to be created with data members and functions

3 sections of C++ program Class declaration Class implementation main() function (We also discussed about the constructor function)

Class implementation Date::Date(int mm, int dd, int yyyy) // constructor function date { month = mm; day = dd; year = yyyy; } void Date::setdate(int mm, int dd, int yyyy) // function setdate return; void Date::showdate(void) // function showdate cout<<month<<day<<year<<endl; return

Class implementation explanation The member functions declared in declaration section are written. :: (scope resolution operator identifies the function as a member of a particular class). The class has 3 functions Date(), setdate(), showdate(). First function has same name as class, called constructor function. It has no return type. It assigns the data members month, day and year with the values of the parameters mm, dd & yyyy respectively.

Class implementation explanation contd... The 2nd function void Date::setdate(int mm, int dd, int yyyy) defines this as the setdate() function belonging to the Date class (Date::). Return type void. It assigns the data members month, day & year with the value of its parameters. The 3rd function void Date::showdate(void) has no parameters, returns no value, and is a member of Date class. Used to display date.

main() function Int main() { Date a, b, c(4, 1, 1998) /* (declare 3objects – initializes one of them) */ b.setdate(12,25,2002); // assign values to b’s data a.showdate(); // display object a’s values b.showdate(); // display object b’s values c.showdate(); // display object c’s values return 0; }

main() function explanation a,b,c are objects of class Date Whenever a new object is defined, memory is allocated for the object and its data members are automatically initialized. This is done by an automatic call to the class constructor function. When object ‘a’ is defined, constructor function Date() and its default values are initialized But in object c, (4, 1, 1998) are initialized because it has parameters assigned.

main() function explanation contd... Therefore, a.month = 7; a.day = 4; a.year = 2001; c.month = 4; c.day = 1; c.year = 1998; b.setdate(12, 25, 2002) calls b’s setdate function, which assigns the argument values 12, 25, 2002 Since all class functions are public, it is valid inside the main function