Lab – 2018/04/12 implement getTotalCount to return how many ‘nMatrix’s are allocated(exist) (modify constructor and destructor, use static variable and.

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

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
CPS 235 Object Oriented Programming Paradigm
Starting Out with C++, 3 rd Edition 1 Chapter 14 – More About Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
Functions Prototypes, parameter passing, return values, activation frams.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
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.
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
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.
C++ Classes & Data Abstraction
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
OBJECT ORIENTED PROGRAMMING Instructor: Rashi Garg Coordinator: Gaurav Saxena.
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
Chapter 05 (Part V) Control Statements: Part II. Nested For-Structures Consider the following codes: for (int i=0; i
Static Keyword. What is static The static keyword is used when a member variable of a class has to be shared between all the instances of the class. All.
C++ Review (3) Structs, Classes, Data Abstraction.
Object Oriented Programming (OOP) Lecture No. 11.
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.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.
Programming in C++ Michal Brabec Petr Malý. Class / Struct Class / Struct consists of: data members function members constructors destructor copy constructor.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
1 2/21/05CS250 Introduction to Computer Science II Destructors, Get and Set, and Default Memberwise Assignment.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04.
1 Introduction to Object Oriented Programming Chapter 10.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
1 Classes struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Object-Oriented Programming (OOP) Lecture No. 24.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends.
Static Variables. Function Scope  “Normal” local variables go out of scope and are deallocated when a function terminates.
Pointers and Dynamic Arrays
Access Functions and Friend Functions
Inheritance II CMSC 202.
14.4 Copy Constructors.
group work #hifiTeam
Polymorphism Lec
More Object Oriented Programming
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
Pointers & Functions.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Static in Classes CSCE 121 J. Michael Moore.
Summary: Abstract Data Type
Introduction to Programming
Dynamic Memory A whole heap of fun….
List Iterator Implementation
Pointers & Functions.
The Stack.
Class: Special Topics Overloading (methods) Copy Constructors
Constructors and Deconstructor
CS 144 Advanced C++ Programming April 30 Class Meeting
ITE “A” GROUP 2 ENCAPSULATION.
Classes Member Qualifiers
CMSC 202 Lesson 17 Inheritance II.
Presentation transcript:

Lab – 2018/04/12 implement getTotalCount to return how many ‘nMatrix’s are allocated(exist) (modify constructor and destructor, use static variable and static function) int main() { nMatrix a(5, 3); cout <<nMatrix::getTotalCount(); << endl; // print 1 nMatrix b(5, 1); nMatrix c(3, 5); cout << nMatrix::getTotalCount(); << endl; // print 3 }

Hint 1 – static variables void demo() { // static variable static int count = 0; cout << count << " "; // value is updated and // will be carried to next // function calls count++; ] int main() { for (int i=0; i<5; i++) demo(); return 0; }

Hint 2 – static variables in a class class GfG { public: static int i; GfG() // Do nothing }; int main() { GfG obj1; GfG obj2; obj1.i = 2; GfG::i = 3; // prints value of i cout << obj1.i<<" "<<obj2.i << endl; cout << GfG::i << endl; }

Hint 3 – static functions in a class class GfG { public: // static member function static void printMsg() cout<<"Welcome to GfG!"; } }; int main() { // invoking a static member function GfG::printMsg(); }