Introduction to Programming

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.
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.
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.
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.
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
Definition Class In C++, the class is the construct primarily used to create objects.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
February 11, 2005 More Pointers Dynamic Memory Allocation.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
 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 (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
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?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Lecture 20 Polymorphism. Introduction General meaning ; the ability to take on different forms. Programming language term: –Allows an entity to take a.
Learners Support Publications Operators.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
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.
Static Variables. Function Scope  “Normal” local variables go out of scope and are deallocated when a function terminates.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Introduction to Programming
Eine By: Avinash Reddy 09/29/2016.
This technique is Called “Divide and Conquer”.
Static Data Member and Functions
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Simple Classes in C# CSCI 293 September 12, 2005.
Programming -2 برمجة -2 المحاضرة-7 Lecture-7.
Counting Loops.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Operators.
Summary: Abstract Data Type
CS150 Introduction to Computer Science 1
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Function “Inputs and Outputs”
Introduction to Programming
Overview of C++ Polymorphism
Introduction to Classes and Objects
Recitation Course 0603 Speaker: Liu Yu-Jiun.
CS410 – Software Engineering Lecture #5: C++ Basics III
Lab – 2018/04/12 implement getTotalCount to return how many ‘nMatrix’s are allocated(exist) (modify constructor and destructor, use static variable and.
CS 144 Advanced C++ Programming February 21 Class Meeting
Object Oriented Programming (OOP) Lecture No. 11
CS 201(Introduction To Programming)
Reading from and Writing to Files Part 2
CS150 Introduction to Computer Science 1
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Constructors & Destructors
Object Oriented Programming (OOP) Lecture No. 12
Object Oriented Programming
Presentation transcript:

Introduction to Programming Lecture 38

Today’s Lecture User Define Manipulator Static key word

User Define Manipulators

Example int i = 10 ; cout << setwidth ( 7 ) << i <<endl ;

Parameter Less Manipulators

cout << manipulator << otherdata ;

ostream & manipulatorName ( ostream & os ) ;

Definition ostream & manipulatorName ( ostream & os ) { return os << userDefinedManipulator ; }

User Defined Manipulators A Short Example // Tab ostream & tab ( ostream & output ) { return output << '\t' ; } // bell ostream & bell ( ostream & output ) return output << '\a' ; // Takes the cursr to next line ostream & endLine ( ostream & output ) return output << '\n' << flush ; void main ( ) cout << "Virtual " << tab << "University" << bell << endLine ; // Use of Mainpulator

Static

Static Are variables which exist for a certain amount of time which is longer than an ordinary automatic variable.

Global

Example int i ; void myfunction ( void ) { for ( i = 0 ; i < 10 ; i ++ ) cout << i << endl ; }

Automatic Variable

State

static int i ;

static int i = 0 ; //Initialization ......... i = 0 ; //Ordinary Assignment Statement

Example void f ( void ) { static int i = 0 ; i ++ ; cout << “Inside function f value of i : ” << i << endl ; } main ( ) int j ; for ( j = 0 ; j < 10 ; j ++ ) ; f ( ) ;

Example class Truck { char a ; public : Truck ( char c ) a = c ; cout << "Inside constructor for object " << a << endl ; } ~ Truck ( ) cout << "Inside destructor for object " << a << endl ; } ;

Example Truck a ( 'A' ) ; main ( ) { Truck b ( 'B' ) ; f ( ) ; g ( ) ; cout << “Function g has been called " << endl ; }

Example void f ( ) { Truck c ( 'C' ) ; } void g ( ) static Truck g ( 'G' ) ;

Example Output Inside constructor for object A Inside constructor for object B Inside constructor for object C Inside destructor for object C Inside constructor for object G function g has been called Inside destructor for object B Inside destructor for object G Inside destructor for object A

Static Data Member Inside A Class

File Scope

Example class Truck { public : int wheels ; int seats ; } ; main ( ) Truck A ; A.seats ; }

Scope Resolution Operator ::

Truck :: nameOfStaticDatamember = values ;

Example class SavingsAccount { } ; private : char name [ 30 ] ; float accountNumber ; float currentBalance ; static float profitRate ; // ... public : SavingsAccount ( ) ; //... } ;

SavingsAccount :: profitRate = 3.0 ;

SavingsAccount A ; A.profitRate ; // bad usage

A.profitRate = 4.0 ; // bad usage

SavingsAccount :: profitRate ;

Example class Student { public : static int howMany ; Student ( ) { howMany ++ ; } ~ Student( ) { howMany -- ; } void displayHowMany ( ) cout << "Number of students are " << howMany ; } } ; int Student :: howMany = 0 ;

Dynamic

What we covered today Parameter Less Manipulation Static Data