Introduction to Programming Lecture 40. Class Class is a user defined data type.

Slides:



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

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
1 CSC241: Object Oriented Programming Lecture No 21.
Introduction to Programming Lecture 39. Copy Constructor.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
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.
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
CMSC 2021 Stream I/O Operators and Friend Functions.
Definition Class In C++, the class is the construct primarily used to create objects.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
1 Data Structures CSCI 132, Spring 2014 Lecture 4 Implementing Life.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6 1.
 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.
11 Introduction to Object Oriented Programming (Continued) Cats.
Object Oriented Programming (OOP) Lecture No. 11.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
CSC241 Object-Oriented Programming (OOP) Lecture No. 7.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
LECTURE 4. Composition Definition: A data member of a class is an object of some other class Example: an AlarmClock object needs to know when it is supposed.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
Review of Last Lecture. What we have learned last lecture? What does a constructor do? What is the way to define a constructor? Can it be defined under.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
Inheritance and Composition Reusing the code and functionality Unit - 04.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
1 2/21/05CS250 Introduction to Computer Science II Destructors, Get and Set, and Default Memberwise Assignment.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
CS 11 C++ track: lecture 6 Today: Default function arguments friend classes Introduction to exception handling.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7: Classes Part II Outline 7.1 Introduction 7.2 const (Constant) Objects and const Member Functions.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Problem Session Working in pairs of two, solve the following problem...
Object Oriented Programming (OOP) Lecture No.2 Downloaded From:
Introduction to Programming
Access Functions and Friend Functions
Introduction to Programming
Object Oriented Programming (OOP) Lecture No. 8
CISC181 Introduction to Computer Science Dr
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
Introduction to Programming
Lecture 8: A Simple System
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction of Programming
Object-Oriented Programming (OOP) Lecture No. 22
Computing Fundamentals with C++
do/while Selection Structure
Recitation Course 0520 Speaker: Liu Yu-Jiun.
CS410 – Software Engineering Lecture #5: C++ Basics III
Object Oriented Programming (OOP) Lecture No. 11
Introduction to Programming
Object oriented programming (OOP) Lecture No. 6
CS 201(Introduction To Programming)
Presentation transcript:

Introduction to Programming Lecture 40

Class Class is a user defined data type.

Data Hiding

Public Interface

Yes you can use user defined data type as a member of the class Class can contain objects.

Example class Date { private : int month, day, year ; int month, day, year ; public : // member functions // member functions } ;

Example class Person { private : private : char * name ; char * name ; char * address ; char * address ; Date dateOfBirth ; // member object Date dateOfBirth ; // member object public : public : // public member functions... // public member functions... } ;

Person :: Person ( char * name, char * address, int day, int month, int year ) { dateOfBirth.setDay ( dy ) ; dateOfBirth.setMonth ( mn ) ; dateOfBirth.setYear ( yr ) ; // statement ( s ) ; }

Initializer List

Person :: Person ( char * name, char * address, int day, int month, int year ) : Date ( int month, int day, int year )

Constructor Date :: Date ( int day, int month, int year ) { cout << “Inside Date constructor ” ; } Person :: Person ( char * name, char * address,int day, int month, int year ) : Date ( month, day, year ) { cout << “Inside Person constructor” ; }

Destructor ~ Date ( ) { cout << “Inside Date destructor ” ; } ~ Person ( ) { cout << “Inside Person destructor” ; }

Example class Column { private : private : int size ; int size ; public : public : Column ( ) Column ( ) { cout << "Column Created" << endl ; cout << "Column Created" << endl ; } void display ( ) ; void display ( ) ; void set ( int ) ; void set ( int ) ; ~ Column ( ) ~ Column ( ) { cout << "Column destroyed" << endl ; cout << "Column destroyed" << endl ; } } ;

Example class Row { private : private : int size ; int size ; Column col ; Column col ; public : public : void set ( int ) ; void set ( int ) ; Row ( ) { cout << "Inside Constructor for object Row" << endl ; cout << "Inside Constructor for object Row" << endl ; } ~ Row ( ) ~ Row ( ) { cout << "Row destroyed" << endl ; cout << "Row destroyed" << endl ; } void display ( ) ; void display ( ) ; } ;

Example class Matrix { private : private : Row row ; Row row ; int size ; int size ; public : public : Matrix ( ) Matrix ( ) { { cout << "Matrix Created" << endl << endl ; cout << "Matrix Created" << endl << endl ; } ~ Matrix ( ) ~ Matrix ( ) { cout << "Matrix destroyed" << endl << endl ; cout << "Matrix destroyed" << endl << endl ; } void display ( ) ; void display ( ) ; } ;

main( ) { Matrix m ; m.display ( ) ; } Example

Column Created Inside Constructor for object Row Matrix Created …. Matrix Destroyed Row Destroyed Column Destroyed Output

Code Reuse

const Date dateOfBirth ;

Structure inside a structure

Class inside a class

Example class Outer {……. class Inner1 class Inner1{ private : // Data members //data functions public : // Data members //data functions } ; //data members //data functions } ;

Class inside a class Outer Inner1 Inner2

Friend

Example class Outer { public : class Inner ; friend class Inner ; class Inner { friend class outer ; // Data members }; // Member functions } ;

ReturnType className :: functionName ( ) { // Statement ( s ) ; definition } }

Outer :: Inner :: Inner ( ) { // definition }

Example class Outer { class Inner1 { class Inner2 { class Inner3 class Inner3 { // Data members and functions // Data members and functions // and perhaps more inner classes // and perhaps more inner classes } ; } ; // Data members and functions // Data members and functions } ; // Data members and functions } ; 48: