Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.

Slides:



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

Recursion.
Operator overloading redefine the operations of operators
For(int i = 1; i
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
Copyright  Hannu Laine C++-programming Part 8 Hannu Laine.
Inheritance (2).
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Write a function to calculate the cubic function: y = 4x 3 + 2x 2 –5x – 4 The function should return y for any given value of x. Question One #include.
Functions Prototypes, parameter passing, return values, activation frams.
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
1 CSC241: Object Oriented Programming Lecture No 21.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
1 Chapter 11 Introducing the Class Pages ( )
Revision.
Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
EC-241 Object-Oriented Programming
Inheritance zThe mechanism by which one class can inherit the properties of another. zIt allows a hierarchy of classes to be built, moving from the most.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
C++ Classes & Data Abstraction
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Introduction of Programming Lecture 28. Today’s Lecture How memory allocation is done in How memory allocation is done in C++ C++ How is it different.
A program example is given below to input date and display on the screen by write a class ‘ date ‘ # include Class date { Private: Int y, m, d ; Publice.
CPSC 231 C++ Review1 Learning Objectives §Review of the object oriented design goals. §Review of C++ classes l data members l member functions (methods)
Chapter 05 (Part V) Control Statements: Part II. Nested For-Structures Consider the following codes: for (int i=0; i
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Objects and Classes Project 2 description.
Structure TDate struct TDate { int year, month, day; }; // Define a new data type.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Overview of Date Functions Getting the current system date Getting the current system time Comparing dates Comparing times.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
ITP © Ron Poet Lecture 15 1 Helper Objects Again.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
NESTED CLASS. Apa itu nested class ? Nested class is a class defined inside a class, that can be used within the scope of the class in which it is defined.
Chapter 9 Type Conversions
Class Definitions and Writing Methods
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
Introduction to Programming
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
Dynamic Memory A whole heap of fun….
Classes.
class PrintOnetoTen { public static void main(String args[]) {
Introduction of Programming
Web Service.
Controlling Program Flow
Continued from last class
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
The Stack.
Lab – 2018/04/12 implement getTotalCount to return how many ‘nMatrix’s are allocated(exist) (modify constructor and destructor, use static variable and.
Introduction to Programming
Object-Oriented Programming (OOP) Lecture No. 23
Thing / Person:____________________ Dates:_________________
How Classes Work with Memory Diagram
Introduction to Algorithms and Programming COMP151
Chapter 11 Classes.
Presentation transcript:

Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data and functions// public data and functions };};

Example class Date {class Date { private :private : int day, month, year ;int day, month, year ; public :public : setMonth ( ) ;setMonth ( ) ; print ( ) ;print ( ) ; };};

Example int main ( ) {int main ( ) { Date mydate ;Date mydate ; mydate.setMonth ( ) ;mydate.setMonth ( ) ; mydate.print ( ) ;mydate.print ( ) ; }

Example class Date {class Date { public :public : void display ( ) ;void display ( ) ; Date ( int day, int month, int year ) ;Date ( int day, int month, int year ) ; private:private: int day, month, year ; int day, month, year ; } ;} ;

Class Functions void Date :: display ( )void Date :: display ( ) { cout << day << / " << month << / " << year ;cout << day << / " << month << / " << year ; } } Scope Resolution OperatorScope Resolution Operator

Example 3 int main ( ) { int main ( ) { Date mydate ;Date mydate ; mydate.display ( ) ;mydate.display ( ) ; } }

Example 3 class Date {class Date { public :public : Date ( int month, int day, int year ) ;Date ( int month, int day, int year ) ; void display ( ) ;void display ( ) ; setDay ( int ) ;setDay ( int ) ; setMonth ( int ) ;setMonth ( int ) ; setYear ( int ) ;setYear ( int ) ; private :private : int month, day, year ;int month, day, year ; } ;} ;

Example 3 void Date :: setDay ( int i )void Date :: setDay ( int i ) { { day = i ;day = i ; }

Example 3 int main ( ) { int main ( ) { Date mydate ;Date mydate ; mydate.setDay ( 10 ) ;mydate.setDay ( 10 ) ; }

Constructor class Date {class Date { public :public : Date ( ) ;Date ( ) ; void display ( ) ;void display ( ) ; private :private : int month, day, year ; int month, day, year ; };}; Date :: Date ( ) { // Body of constructor }Date :: Date ( ) { // Body of constructor }

Constructor A constructor is a class member function with same name as the class This function is invoked (called) automatically each time when the object of that class is created(instantiated) Constructor must be in pubic area of that class

class Date {class Date { public :public : Date ( ) ;Date ( ) ; void display ( ) ;void display ( ) ; private :private : int month, day, year ; int month, day, year ; };}; Date :: Date ( ) { // Body of constructor }Date :: Date ( ) { // Body of constructor } int main ( ) {int main ( ) { Date d1,d2,d3;Date d1,d2,d3; mydate.display ( ) ;mydate.display ( ) ; } }

Constructor When a class object is created,its member variables can be initialized in that classs constructor Date :: Date ( ) { month=day=year=0; }Date :: Date ( ) { month=day=year=0; } Constructor can have argumentsConstructor can have arguments Constructor with no argument is called default constructorConstructor with no argument is called default constructor

Constructor class Date {class Date { public :public : Date ( int month, int day, int year ) ;Date ( int month, int day, int year ) ; void display ( ) ;void display ( ) ; private :private : int month, day, year ; int month, day, year ; };}; Date :: Date ( int month, int day, int year ) { // Body of the function }Date :: Date ( int month, int day, int year ) { // Body of the function }

Example 3 int main ( ) { int main ( ) { Date mydate ( 1, 1,2002 ) ;Date mydate ( 1, 1,2002 ) ; mydate.display ( ) ;mydate.display ( ) ; } } We can set default argument for constructorWe can set default argument for constructor Date :: Date ( int day, int month=9, int year = 2002 )Date :: Date ( int day, int month=9, int year = 2002 )

Example 3 main ( ) {main ( ) { Date mydate ( 1, 1,2006 ) ;Date mydate ( 1, 1,2006 ) ; Date mydate ( 1, 1 ) ;Date mydate ( 1, 1 ) ; Date mydate ( 1 ) ;Date mydate ( 1 ) ;} Constructors can be overloadedConstructors can be overloaded

Rules of function overloading Whenever we overload a function, the name of the function remain the same but argument list changes.Whenever we overload a function, the name of the function remain the same but argument list changes. The argument list can:The argument list can: Either vary in the number of argumentsEither vary in the number of arguments Or vary in the typeOr vary in the type

#include class Date { public : Date ( ) ; Date ( int, int, int=2005 ) ; void show(); private: int month, day, year ; } ; Date::Date( ){ cout<<"\nDefault constructure is called"; }

Date::Date(int d,int m,int y ){ cout<<"\nArgument constructure is called"; month=m; day=d; year=y; } void Date::show(){ cout<<endl<<month<<"-"<<day<<"- "<<year; }

The main Function void main(){ Date a,b(10,26,2004),c(10,26); a.show(); b.show(); c.show(); getch(); }

Destructor(~) A destructor is called when the object of that class is destroyed Object is destroyed when program execution leaves the the scope in which that object is created The destructor is written, tilde(~) character followed by class name ~Date ( ) ;~Date ( ) ;

Rules of Destructor Destructors cannot be overloadedDestructors cannot be overloaded Destructors take no argumentsDestructors take no arguments They dont return a valueThey dont return a value

class Date {class Date { public :public : Date ( ) ;Date ( ) ; Date ( int month, int day, int year ) ;Date ( int month, int day, int year ) ; ~Date ( ) ;~Date ( ) ; setMonth ( int month ) ;setMonth ( int month ) ; setDay ( int day ) ;setDay ( int day ) ; setYear ( int year ) ;setYear ( int year ) ; setDate (int day, int month, int year ) ;setDate (int day, int month, int year ) ; private:private: int month, day, year ;int month, day, year ; } ;} ;

class CreateAndDestroy { public : CreateAndDestroy(int) ; ~CreateAndDestroy( ) ; private: int data ; } ; CreateAndDestroy::CreateAndDestroy(int value){ data=value; cout<<"Object "<<data<<" constructor"; } CreateAndDestroy::~CreateAndDestroy(){ cout<<"Object "<<data<<" destructor"; }

void create(); CreateAndDestroy first(1); int main(){ cout<<" (object created outside main)"<<endl; CreateAndDestroy second(2); cout<<" (first object created inside main)"<<endl; create(); CreateAndDestroy third(3); cout<<" (second object created inside main)"<<endl; return 0; } void create(){ CreateAndDestroy fourth(4); cout<<" (first object created create funtion)"<<endl; }