CIS 101: Computer Programming and Problem Solving Lecture11 Usman Roshan Department of Computer Science NJIT.

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
Wrap Up and Misc Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 6 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
Constructors & Destructors Review CS 308 – Data Structures.
CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
Class Byteline Ustyugov Dmitry MDSP November, 2009.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
More C++ Features True object initialisation
Session 7 Methods Strings Constructors this Inheritance.
CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress.
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.
1 CS161 Introduction to Computer Science Topic #16.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
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.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
1 CSC241: Object Oriented Programming Lecture No 17.
1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Learners Support Publications Constructors and Destructors.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
1 Class 19 Chapter 13 – Creating a class definition.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Constructors and Destructors
Procedural and Object-Oriented Programming
Learning Objectives Pointers as dada members
Overloading Operator MySting Example
Concepts of Constructors and Its Types
Chapter 5 Classes.
14.4 Copy Constructors.
Classes & Objects: Examples
Classes Copy Constructors
Local Variables, Global Variables and Variable Scope
Constructors and Destructors
Summary: Abstract Data Type
Introduction to Programming
CS148 Introduction to Programming II
String.
Introduction to Classes and Objects
Java Programming Language
Review: C++ class represents an ADT
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CS 201(Introduction To Programming)
Objects as Function Arguments
Constructors & Destructors
CS 144 Advanced C++ Programming April 30 Class Meeting
Classes Class Data Members & Initializers
Unit-2 Objects and Classes
Presentation transcript:

CIS 101: Computer Programming and Problem Solving Lecture11 Usman Roshan Department of Computer Science NJIT

Classes Let’s say we want to define a rectangle object. This is the same defining a new type of variable called rectangle. Previous types we have seen so far are int, char, float, and their pointers. int length; int width; int area(); float diagonal(); This is our rectangle class. it contains the length and width, and also functions to compute the area and diagonal. Rectangle

Defining classes class { public: ; … private: … };

Rectangle class

Constructors, destructors, and copy constructor Constructor: get called when a new instance of the object is created. –rectangle myrec; //constructor gets called Destructor: gets called when you exit the scope of the variable –void myfunction(){ rectangle myrec; myrec.setlength(2); myrec.setwidth(1); myrec.print(); –} //once we exit this function the destructor gets called Copy constructor: gets called everytime a copy of the object is created –rectangle myrec2 = myrec;//not called in this case, we’ll have to write a //separate function for this which (if time) we may see later –void myfunction(rectangle myrec); –void main(){ rectangle myrec; myfunction(myrec) //at this point a copy of myrec is created that is local to //myfunction –}–}

Constructor Everytime we create a rectangle its length and width are automatically set to 2 and 2.

Overloading constructor You can have two or more constructors: convert and default Also called function overloading

Copy constructor Implicit copy constructor is called which copies width and length of r1 into width and length of r2.

String class

Strings mystring p size7 string(int s){ size = s; p = new char[size]; } string mystring(7); string( string& from) { size = from.size; p = from.p; } string mystring2(mystring); mystring2 p size7

Strings mystring p size7 string(int s){ size = s; p = new char[size]; } string mystring(7); string( string& from) { size = from.size; p = new char[size]; for(int i=0;i<size;i++){ p[i]=from.p[i]; } string mystring2(mystring); mystring2 p size7

Lab problems 1.Create string class and add functions: 1.Copy string 2.Compare string for equality 3.Concatenate strings 4.Substring 2.Create Vector class 1.Copy vector 2.Vector equality 3.Dot product 4.Euclidean norm (length) of vector