Scope. Scope is such an important thing that we ll review what we know about scope now:

Size: px
Start display at page:

Download "Scope. Scope is such an important thing that we ll review what we know about scope now:"

Transcription

1 Scope Scope is such an important thing that we ll review what we know about scope now: Local (block) scope: A name declared within a block is accessible only within that block and blocks enclosed by it, and only after the point of declaration. The names of formal arguments to a function in the scope of the outermost block of the function have local scope, as if they had been declared inside the block enclosing the function body. File scope: Any name declared outside all blocks or classes has file scope. It is accessible anywhere in the translation unit (file) after its declaration. Names with file scope that do not declare static objects are often called global names. Function scope: Labels are the only names that have function scope. They can be used anywhere within a function but are not accessible outside that function. Prototype scope: Names declared in a function prototype are visible only until the end of the prototype. 1

2 Class Scope More about class scope now: data members and function members have class scope this means that they can be accessed within the class s methods by name outside a class s scope, class members cannot be referenced by name but the public members can be referenced by a handle an object name a reference to an object (remember this is an alias) a pointer or tracking handle to an object The point is that the system has to know WHICH day s weather you are talking about, for example Practicalities: access through an object name or through a reference use the dot (.) access operator access through a pointer or tracking handle to the object uses the arrow (->) access operator Some variables (those defined within a method) have only method scope 2

3 Class Scope - Examples remember that names of class members have class scope. Class member functions can be accessed only by using the member-selection operators (. or ->) or pointer-to-member operators (.* or ->*) on an object or pointer to an object of that class weather today = *(new weather()); weather *monday; double degday; today.settemp (22, 12); monday = &today; degday = monday -> degreedays(); only legal if the assignment operator is defined for weather objects!! all nonstatic class member data is considered local to the object of that class. 3

4 Making Instances of Classes (Objects) We ve seen that objects can be made: When an object of a particular class is declared When the operator new is used to make one In either case, the function that actually initializes the object is a constructor for the class Constructors initialize the instance variables of objects There could be multiple constructors for a class - overloaded constructors can be used to provide different ways to initialize objects of a class Even if the constructor does not do so explicitly, all data members are initialized Primitive numeric types are set to 0 Boolean types are set to false Pointers are set to null If you don t define a constructor for a class, a default constructor is provided It has no code and takes no parameters there can only be one default constructor (i.e. one with no arguments) for each class 4

5 weather::weather(int a, int b) {settemps (a, b); Remember the class weather class weather { public: weather(int, int); weather(); void settemps (int, int); double avgtemp (); double degreedays (); void printdata (); weather::weather() { settemps (0, 0); void weather::settemps (int max, int min) { maxtemp = (max >= min? max : min); private: mintemp = (min <= max? min : max); int maxtemp; int mintemp; double weather::avgtemp () ; { return ((maxtemp - static_cast <double> (mintemp))/2.0 + mintemp); double weather::degreedays () { return (65 - ((maxtemp - static_cast <double>(mintemp))/2.0 + mintemp)); void weather::printdata() { cout << "\n the max temp is " << maxtemp << " and the min temp is " << mintemp << endl; 5

6 Understanding What Happens if Suppose that we re dealing with the weather class and we have the following code: weather d1, d2; d1.settemps(21, 0); d2 = d1; What happens here? The declaration of d1 and d2 causes space for both of these variables to be allocated and also calls the default constructor to give the data members default values. The system has built an overloaded assignment operator for you it does component-wise assignment (and it works just fine because the members are int type things) 6

7 What if we have data elements that are pointers or tracking handles (for managed stuff)? Consider the following scenario: ref class MemberInfo { public: MemberInfo(); void entername(); void enterage(); void display(); private: String ^name; //notice the dynamic member int age; ; The dynamic member is a String that is a managed type and the class is a reference class!! (note the ref in the definition)! 7

8 Default Constructor for the class MemberInfo We may as well define a default constructor for this class, and this one will do: MemberInfo::MemberInfo(){ name = "no name"; age = 0; ref class MemberInfo { public: MemberInfo(); void entername(); void enterage(); void display(); private: String ^name; int age; ; 8

9 And member function definitions: void MemberInfo::enterName(){ Console::Write( Enter member s name: ); this -> name = Console::ReadLine(); void MemberInfo::enterAge(){ Console::Write( Enter member s age: ); this -> age = Int32::Parse(Console::ReadLine()); ref class MemberInfo { public: MemberInfo(); void entername(); void enterage(); void display(); private: String ^name; int age; ; Notice the use of the this pointer! void MemberInfo::display(){ Console::WriteLine( Member s name is: {0 and age is {1, this -> name, this -> age.tostring()); Every object has access to a pointer to itself called the this pointer that can be used to implicitly reference the data members and methods of an object 9 from the methods of that object

10 Can we do a better default constructor? Instead of MemberInfo::MemberInfo(){ name = "no name"; age = 0; Could we do this? MemberInfo::MemberInfo(){ entername(); enterage(); 10

11 A driving function using MemberInfo: int main() { MemberInfo ^n1, ^n2; //declare two tracking handles n1 = gcnew(memberinfo); //make a new object n1 -> entername(); n1 -> enterage(); n1 -> display(); n2 = n1; n2 -> display(); Console::ReadLine(); return 0; This works just fine!! 11

12 But if we change it like this: int main() { MemberInfo ^n1, n2; n1 = gcnew(memberinfo); n1 -> getname(); n1 -> getage(); n1 -> display(); n2 = *n1; n2.display(); return 0; //declare a tracking pointer and a //variable //make a new object We ll get an error: operator = is unavailable in MemberInfo The reason for this is that the assignment operator has no automatic override due to the dynamic data! 12

13 Recap: the class MemberInfo (expanded a bit) #pragma once #ifndef MEMB1 #define MEMB1 using namespace System; Class Definition Goes into the header file ref class MemberInfo{ public: MemberInfo();// default constructor void entername();// method for entering name void enterage();// method for entering age String^ getname();// method for extracting name int getage();// method for extracting age void setname(string^);// method for setting name to a value void setage(int);// method for setting age to a value void display();// method for display of the object s data private: String ^name; int age; static int count; ; #endif 13

14 #include "stdafx.h #include <iostream> #include "MemberInfo.h" using namespace System; using namespace std; // the default constructor MemberInfo::MemberInfo(){ name = "no name"; age = 0; count++; // set name to a value indicated in operand void MemberInfo::setName (String^ s){ name = s; // set age to a value indicated in operand void MemberInfo::setAge (int a){ age = a; // accept name entered by user void MemberInfo::enterName(){ Console::WriteLine("Enter the member's name: "); this->name = Console::ReadLine(); //name = Console::ReadLine(); Method definitions for the class Goes in the.cpp file // accept age entered by user void MemberInfo::enterAge(){ Console::Write ("Enter the member's age: "); this->age = Int32::Parse(Console::ReadLine()); // return value of the name data member String ^ MemberInfo::getName(){ return name; // return value of the age data member int MemberInfo::getAge(){ return age; // display the object s data members void MemberInfo::display(){ Console::WriteLine("Member Name: {0 and age = {1", name, age.tostring()); Console::WriteLine("Number of Members: {0", count.tostring()); 14

15 Classes with Data Members that are Pointers In native C++, when a data member is a pointer, the constructor must allocate space for it and a destructor must deallocate space (otherwise we get memory leaks). Similarly when the class is managed the constructor has to allocate but the gc cleans up. Whenever a class has at least one data member that is a pointer, you MUST write a destructor a copy constructor an overloaded assignment operator Otherwise you will get error messages 15

16 Destructors A destructor is a function that: Is automatically invoked when an object is destroyed i.e. at the end of the block in which an object was declared Performs termination housekeeping In particular, it recovers memory to be reused Takes no arguments and returns no value Has the same name as the class preceded by a ~ There is only one destructor for a class no overloading is possible The system automatically generates a destructor for a class, but Programmers do have to write a destructor when their class dynamically allocates memory (which can happen when data members are pointers) Even if the class is a managed class, a destructor should be written! 16

17 Classes with Data Members that are Pointers Now to the concept of a copy constructor: There are certain situations when a copy of an object must be made: when passing the object by value when returning an object Furthermore, we want to be able to initialize an object by another object. Creating a copy of an object is the job of the copy constructor. This is a special kind of constructor that takes as argument an object of the same type as the class. So why do you have to write one when a data member is a pointer? 17

18 First, why do we need a copy constructor? If a copy constructor is missing (for any standard C++ class), the compiler creates a default copy constructor that performs member-by-member copying. If we tried to consider a default for the MemberInfo class, the default might look like: MemberInfo (const MemberInfo& init) { name = init.name; age = init.age But this can t work let s see why suppose we trace the activity in the following: MemberInfo ^president = gcnew MemberInfo ( Obama, 44); MemberInfo clone(^president); delete [] president; president = NULL; 18

19 Trace step 1: MemberInfo ^president = gcnew MemberInfo ( Obama, 44); president 44 O B A M A 19

20 Trace step 2: MemberInfo clone(^president); Let's look inside the copy constructor: name = init.name; age = init.age president O B A M A 44 clone 44 We need a way to make the two objects be independent objects! Let s see why -- 20

21 Trace step 3: (for the managed case) President = NULL; 44 president OBAMA clone 44 Managed datatypes are automatically garbage collected this does not happen in standard C++!! that means that the delete[] president; was not needed! 21

22 Trace step 3: (native code unmanaged) Delete [] president; president = NULL; In standard C++ (using a standard C string for the name) you d wind up with this scenario on deletion of president. president clone 44 Freed space The two objects really need to be independent objects, so we need to provide a copy constructor can t use the default! MemberInfo::MemberInfo ( MemberInfo %s) {String^ str = s.name; name = str; age = s.age; 22

23 This is related to Assignment the = Operator: What Happens by Default Ordinarily, the compiler will generate a default assignment operator a point class (that has two double data members) is an example of a case in which the = operator is automatically overloaded by the compiler If you define any assignment operator that takes the class as a parameter, the compiler cannot generate a default assignment operator for that class. In these cases, you have to provide an overloaded assignment operator 23

24 Class with Very Simple Destructor, Copy Constructor and Operator = overloaded Suppose that we have a class point with header: class point { ; public: point (); point (double x, double y ); void setpoint(double, double); double get_x (); double get_y (); ~point(); // default constructor // overloaded constructor // sets the point s value // destructor point (point &p); // copy constructor point & operator = (point &p); // overloading of = private: double x; double y; 24 pointclass example

25 Class with Very Simple Destructor, Copy Constructor and Operator = overloaded Suppose that we have a class point with header: class point { ; public: point (); point (double x, double y ); void setpoint(double, double); double get_x (); double get_y (); ~point(); // default constructor // overloaded constructor // sets the point s value // destructor point (point &p); // copy constructor point & operator = (point &p); // overloading of = private: double x; double y; 25 pointclass example

26 Constructor and Destructor Methods for class point point::point(){ cout << "default constructor \n"; x = 0.0; y = 0.0; //default constructor point::point (double xval, double yval) // overloaded constructor { cout << "overloaded constructor with values " << xval << ", " << yval << endl; setpoint (xval, yval); point::~point(){ // destructor - no body needed here cout << "destructor \n"; // destructor 26

27 Mutator and Inspector methods for class point // mutator method void point::setpoint(double xval, double yval){ cout << "setpoint with values " << xval << ", " << yval << endl; x = xval; y = yval; // accessor methods double point::get_x(){ return x; double point::get_y(){ return y; 27

28 Copy Constructor, and Overloaded = operator point::point (point &p){ // copy constructor double xval = p.get_x(); double yval = p.get_y(); cout << "copy constructor with " << xval << ", " << yval << endl; setpoint (xval, yval); point & point::operator = (point &p){ // overloading of = cout << "overloaded = operator" << endl; double x = p.get_x(); double y = p.get_y(); setpoint(x, y); return *this; 28

29 With driving function: int main() { point p1, p2; point p3(1.5, 25.5); p1.setpoint(2.2, 3.3); p2 = p1; point p4(p3); return 0; Notice that these are declarations of objects of type point 1. default constructor called to create p1 2. default constructor called to create p2 3. overloaded constructor called to create p3 4. setpoint method called to set new values for p1 5. overloaded assignment operator called to set a new value for p2 6. copy constructor called to create a copy of p3 in p4 29

30 With driving function: int main() { point p1, p2; point p3(1.5, 25.5); p1.setpoint(2.2, 3.3); p2 = p1; point p4(p3); return 0; 30

Function Overloading

Function Overloading Function Overloading C++ supports writing more than one function with the same name but different argument lists How does the compiler know which one the programmer is calling? They have different signatures

More information

Implementing an ADT with a Class

Implementing an ADT with a Class Implementing an ADT with a Class the header file contains the class definition the source code file normally contains the class s method definitions when using Visual C++ 2012, the source code and the

More information

Where do we stand on inheritance?

Where do we stand on inheritance? In C++: Where do we stand on inheritance? Classes can be derived from other classes Basic Info about inheritance: To declare a derived class: class :public

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Chapter 9 Initializing a non-static data member in the class definition is a syntax error 1 9.2 Time Class Case Study In Fig. 9.1, the class definition is enclosed in the following

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

The Class Construct Part 2

The Class Construct Part 2 The Class Construct Part 2 Lecture 24 Sections 7.7-7.9 Robb T. Koether Hampden-Sydney College Mon, Oct 29, 2018 Robb T. Koether (Hampden-Sydney College) The Class Construct Part 2 Mon, Oct 29, 2018 1 /

More information

CSC1322 Object-Oriented Programming Concepts

CSC1322 Object-Oriented Programming Concepts CSC1322 Object-Oriented Programming Concepts Instructor: Yukong Zhang February 18, 2016 Fundamental Concepts: The following is a summary of the fundamental concepts of object-oriented programming in C++.

More information

pointers & references

pointers & references pointers & references 1-22-2013 Inline Functions References & Pointers Arrays & Vectors HW#1 posted due: today Quiz Thursday, 1/24 // point.h #ifndef POINT_H_ #define POINT_H_ #include using

More information

inside: THE MAGAZINE OF USENIX & SAGE August 2003 volume 28 number 4 PROGRAMMING McCluskey: Working with C# Classes

inside: THE MAGAZINE OF USENIX & SAGE August 2003 volume 28 number 4 PROGRAMMING McCluskey: Working with C# Classes THE MAGAZINE OF USENIX & SAGE August 2003 volume 28 number 4 inside: PROGRAMMING McCluskey: Working with C# Classes & The Advanced Computing Systems Association & The System Administrators Guild working

More information

CS11 Introduction to C++ Fall Lecture 1

CS11 Introduction to C++ Fall Lecture 1 CS11 Introduction to C++ Fall 2006-2007 Lecture 1 Welcome! 8 Lectures (~1 hour) Slides posted on CS11 website http://www.cs.caltech.edu/courses/cs11 7 Lab Assignments on course website Available on Monday

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

Introduction Of Classes ( OOPS )

Introduction Of Classes ( OOPS ) Introduction Of Classes ( OOPS ) Classes (I) A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class.

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Function Declarations. Reference and Pointer Pitfalls. Overloaded Functions. Default Arguments

Function Declarations. Reference and Pointer Pitfalls. Overloaded Functions. Default Arguments Reference and Pointer Pitfalls Function Declarations Never return a reference or a pointer to a local object. The return value will refer to memory that has been deallocated and will be reused on the next

More information

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University (5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University Key Concepts Function templates Defining classes with member functions The Rule

More information

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

Object Reference and Memory Allocation. Questions:

Object Reference and Memory Allocation. Questions: Object Reference and Memory Allocation Questions: 1 1. What is the difference between the following declarations? const T* p; T* const p = new T(..constructor args..); 2 2. Is the following C++ syntax

More information

CS 162 Intro to CS II. Structs vs. Classes

CS 162 Intro to CS II. Structs vs. Classes CS 162 Intro to CS II Structs vs. Classes 1 Odds and Ends Assignment 1 questions Why does the delete_info have a double pointer to states as a parameter? Do your functions have to be 15 or under? Anymore???

More information

Assumptions. History

Assumptions. History Assumptions A Brief Introduction to Java for C++ Programmers: Part 1 ENGI 5895: Software Design Faculty of Engineering & Applied Science Memorial University of Newfoundland You already know C++ You understand

More information

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved. Chapter 10 Pointers and Dynamic Arrays Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives Pointers Pointer variables Memory management Dynamic Arrays Creating and using Pointer arithmetic

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #12 Apr 3 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Intro CPP Boring stuff: Language basics: identifiers, data types, operators, type conversions, branching

More information

OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING Classes and Objects So far you have explored the structure of a simple program that starts execution at main() and enables you to declare local and global variables and constants and branch your execution

More information

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2 Discussion 1E Jie(Jay) Wang Week 10 Dec.2 Outline Dynamic memory allocation Class Final Review Dynamic Allocation of Memory Recall int len = 100; double arr[len]; // error! What if I need to compute the

More information

CS201- Introduction to Programming Current Quizzes

CS201- Introduction to Programming Current Quizzes CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements

More information

Chapter 2. Procedural Programming

Chapter 2. Procedural Programming Chapter 2 Procedural Programming 2: Preview Basic concepts that are similar in both Java and C++, including: standard data types control structures I/O functions Dynamic memory management, and some basic

More information

CS11 Intro C++ Spring 2018 Lecture 3

CS11 Intro C++ Spring 2018 Lecture 3 CS11 Intro C++ Spring 2018 Lecture 3 C++ File I/O We have already seen C++ stream I/O #include cout > name; cout

More information

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Classes and Objects. Class scope: - private members are only accessible by the class methods. Class Declaration Classes and Objects class class-tag //data members & function members ; Information hiding in C++: Private Used to hide class member data and methods (implementation details). Public

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 7 September 21, 2016 CPSC 427, Lecture 7 1/21 Brackets Example (continued) Storage Management CPSC 427, Lecture 7 2/21 Brackets Example

More information

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 12: Classes and Data Abstraction Objectives In this chapter, you will: Learn about classes Learn about private, protected,

More information

Lecture 7. Log into Linux New documents posted to course webpage

Lecture 7. Log into Linux New documents posted to course webpage Lecture 7 Log into Linux New documents posted to course webpage Coding style guideline; part of project grade is following this Homework 4, due on Monday; this is a written assignment Project 1, due next

More information

Operator overloading

Operator overloading 1 Introduction 2 The copy constructor 3 Operator Overloading 4 Eg 1: Adding two vectors 5 The -> operator 6 The this pointer 7 Overloading = 8 Unary operators 9 Overloading for the matrix class 10 The

More information

Pointers, Dynamic Data, and Reference Types

Pointers, Dynamic Data, and Reference Types Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple

More information

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo Chapter 10 Pointers and Dynamic Arrays 1 Learning Objectives Pointers Pointer variables Memory management Dynamic Arrays Creating and using Pointer arithmetic Classes, Pointers, Dynamic Arrays The this

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

2. It is possible for a structure variable to be a member of another structure variable.

2. It is possible for a structure variable to be a member of another structure variable. FORM 1(put name, form, and section number on scantron!!!) CS 162 Exam I True (A) / False (B) (2 pts) 1. What value will the function eof return if there are more characters to be read in the input stream?

More information

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference Pass by Value More Functions Different location in memory Changes to the parameters inside the function body have no effect outside of the function. 2 Passing Parameters by Reference Example: Exchange

More information

An Introduction to C++

An Introduction to C++ An Introduction to C++ Introduction to C++ C++ classes C++ class details To create a complex type in C In the.h file Define structs to store data Declare function prototypes The.h file serves as the interface

More information

Pointers and Dynamic Memory Allocation

Pointers and Dynamic Memory Allocation Pointers and Dynamic Memory Allocation ALGORITHMS & DATA STRUCTURES 9 TH SEPTEMBER 2014 Last week Introduction This is not a course about programming: It s is about puzzling. well.. Donald Knuth Science

More information

CS11 Intro C++ Spring 2018 Lecture 1

CS11 Intro C++ Spring 2018 Lecture 1 CS11 Intro C++ Spring 2018 Lecture 1 Welcome to CS11 Intro C++! An introduction to the C++ programming language and tools Prerequisites: CS11 C track, or equivalent experience with a curly-brace language,

More information

C++ Constructor Insanity

C++ Constructor Insanity C++ Constructor Insanity CSE 333 Spring 2018 Instructor: Justin Hsia Teaching Assistants: Danny Allen Dennis Shao Eddie Huang Kevin Bi Jack Xu Matthew Neldam Michael Poulain Renshu Gu Robby Marver Waylon

More information

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am CMSC 202 Section 010x Spring 2007 Computer Science II Final Exam Name: Username: Score Max Section: (check one) 0101 - Justin Martineau, Tuesday 11:30am 0102 - Sandeep Balijepalli, Thursday 11:30am 0103

More information

CS 11 C++ track: lecture 1

CS 11 C++ track: lecture 1 CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account http://www.cs.caltech.edu/cgi-bin/ sysadmin/account_request.cgi Need to know UNIX (Linux) www.its.caltech.edu/its/facilities/labsclusters/

More information

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

C11: Garbage Collection and Constructors

C11: Garbage Collection and Constructors CISC 3120 C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017 CUNY Brooklyn College 1 Outline Recap Project progress and lessons

More information

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE? 1. Describe History of C++? The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was doing work for his Ph.D. thesis. One of the languages Stroustrup had the opportunity

More information

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington CSE 333 Lecture 10 - references, const, classes Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington Administrivia New C++ exercise out today, due Friday morning

More information

Abstract Data Types (ADT) and C++ Classes

Abstract Data Types (ADT) and C++ Classes Abstract Data Types (ADT) and C++ Classes 1-15-2013 Abstract Data Types (ADT) & UML C++ Class definition & implementation constructors, accessors & modifiers overloading operators friend functions HW#1

More information

System Programming. Practical Session 9. C++ classes

System Programming. Practical Session 9. C++ classes System Programming Practical Session 9 C++ classes C++ parameter passing void incval(int n) { //By value n++; void incpoint(int *p) { //By pointer *p = 5; p = 0; void incref(int &n) { //By Reference cout

More information

CS105 C++ Lecture 7. More on Classes, Inheritance

CS105 C++ Lecture 7. More on Classes, Inheritance CS105 C++ Lecture 7 More on Classes, Inheritance " Operator Overloading Global vs Member Functions Difference: member functions already have this as an argument implicitly, global has to take another parameter.

More information

Introducing C++ to Java Programmers

Introducing C++ to Java Programmers Introducing C++ to Java Programmers by Kip Irvine updated 2/27/2003 1 Philosophy of C++ Bjarne Stroustrup invented C++ in the early 1980's at Bell Laboratories First called "C with classes" Design Goals:

More information

Introduction to Classes

Introduction to Classes Introduction to Classes Procedural and Object-Oriented Programming Procedural and Object-Oriented Programming Procedural programming focuses on the process/actions that occur in a program Object-Oriented

More information

Cpt S 122 Data Structures. Introduction to C++ Part II

Cpt S 122 Data Structures. Introduction to C++ Part II Cpt S 122 Data Structures Introduction to C++ Part II Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Objectives Defining class with a member function

More information

Ch 2 ADTs and C++ Classes

Ch 2 ADTs and C++ Classes Ch 2 ADTs and C++ Classes Object Oriented Programming & Design Constructing Objects Hiding the Implementation Objects as Arguments and Return Values Operator Overloading 1 Object-Oriented Programming &

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

Constants, References

Constants, References CS 246: Software Abstraction and Specification Constants, References Readings: Eckel, Vol. 1 Ch. 8 Constants Ch. 11 References and the Copy Constructor U Waterloo CS246se (Spring 2011) p.1/14 Uses of const

More information

Object-Oriented Programming

Object-Oriented Programming - oriented - iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018 1 / 56 Overview - oriented 1 2 -oriented 3 4 5 6 7 8 Static and friend elements 9 Summary 2 / 56 I - oriented was initially created by Bjarne

More information

Polymorphism Part 1 1

Polymorphism Part 1 1 Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid

More information

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4 CS32 - Week 4 Umut Oztok Jul 15, 2016 Inheritance Process of deriving a new class using another class as a base. Base/Parent/Super Class Derived/Child/Sub Class Inheritance class Animal{ Animal(); ~Animal();

More information

Ch02. True/False Indicate whether the statement is true or false.

Ch02. True/False Indicate whether the statement is true or false. Ch02 True/False Indicate whether the statement is true or false. 1. The base class inherits all its properties from the derived class. 2. Inheritance is an is-a relationship. 3. In single inheritance,

More information

Overloading Operators in C++

Overloading Operators in C++ Overloading Operators in C++ C++ allows the programmer to redefine the function of most built-in operators on a class-by-class basis the operator keyword is used to declare a function that specifies what

More information

Recharge (int, int, int); //constructor declared void disply();

Recharge (int, int, int); //constructor declared void disply(); Constructor and destructors in C++ Constructor Constructor is a special member function of the class which is invoked automatically when new object is created. The purpose of constructor is to initialize

More information

AN OVERVIEW OF C++ 1

AN OVERVIEW OF C++ 1 AN OVERVIEW OF C++ 1 OBJECTIVES Introduction What is object-oriented programming? Two versions of C++ C++ console I/O C++ comments Classes: A first look Some differences between C and C++ Introducing function

More information

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OPERATOR OVERLOADING KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Dynamic Memory Management

More information

Interview Questions of C++

Interview Questions of C++ Interview Questions of C++ Q-1 What is the full form of OOPS? Ans: Object Oriented Programming System. Q-2 What is a class? Ans: Class is a blue print which reflects the entities attributes and actions.

More information

GEA 2017, Week 4. February 21, 2017

GEA 2017, Week 4. February 21, 2017 GEA 2017, Week 4 February 21, 2017 1. Problem 1 After debugging the program through GDB, we can see that an allocated memory buffer has been freed twice. At the time foo(...) gets called in the main function,

More information

Object Oriented Programming in C#

Object Oriented Programming in C# Introduction to Object Oriented Programming in C# Class and Object 1 You will be able to: Objectives 1. Write a simple class definition in C#. 2. Control access to the methods and data in a class. 3. Create

More information

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V.

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V. topics: arrays pointers arrays of objects resources: cis15 advanced programming techniques, using c++ summer 2008 lecture # V.1 some of this lecture is covered in parts of Pohl, chapter 3 arrays review

More information

Functions, Arrays & Structs

Functions, Arrays & Structs Functions, Arrays & Structs Unit 1 Chapters 6-7, 11 Function Definitions! Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where a parameter is: datatype identifier

More information

Chapter 13: Copy Control. Overview. Overview. Overview

Chapter 13: Copy Control. Overview. Overview. Overview Chapter 13: Copy Control Overview The Copy Constructor The Assignment Operator The Destructor A Message-Handling Example Managing Pointer Members Each type, whether a built-in or class type, defines the

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods. Classes: Methods, Constructors, Destructors and Assignment For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Piyush Kumar Classes: Member functions // classes

More information

Pointers. Developed By Ms. K.M.Sanghavi

Pointers. Developed By Ms. K.M.Sanghavi Pointers Developed By Ms. K.M.Sanghavi Memory Management : Dynamic Pointers Linked List Example Smart Pointers Auto Pointer Unique Pointer Shared Pointer Weak Pointer Memory Management In order to create

More information

IS0020 Program Design and Software Tools Midterm, Fall, 2004

IS0020 Program Design and Software Tools Midterm, Fall, 2004 IS0020 Program Design and Software Tools Midterm, Fall, 2004 Name: Instruction There are two parts in this test. The first part contains 22 questions worth 40 points you need to get 20 right to get the

More information

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND Introduction to Classes 13.2 The Class Unit 4 Chapter 13 CS 2308 Fall 2016 Jill Seaman 1 l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains

More information

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; } Multiple choice questions, 2 point each: 1. What output is produced by the following program? #include int f (int a, int &b) a = b + 1; b = 2 * b; return a + b; int main( ) int x=1, y=2, z=3;

More information

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming Chapter 13: Introduction to Classes 1 13.1 Procedural and Object-Oriented Programming 2 Procedural and Object-Oriented Programming Procedural programming focuses on the process/actions that occur in a

More information

C10: Garbage Collection and Constructors

C10: Garbage Collection and Constructors CISC 3120 C10: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/5/2018 CUNY Brooklyn College 1 Outline Recap OOP in Java: composition &

More information

Chapter 9 Classes : A Deeper Look, Part 1

Chapter 9 Classes : A Deeper Look, Part 1 Chapter 9 Classes : A Deeper Look, Part 1 C++, How to Program Deitel & Deitel Fall 2016 CISC1600 Yanjun Li 1 Time Class Case Study Time Class Definition: private data members: int hour; int minute; int

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer September 26, 2016 OOPP / C++ Lecture 4... 1/33 Global vs. Class Static Parameters Move Semantics OOPP / C++ Lecture 4... 2/33 Global Functions

More information

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform Ch 13: Introduction to Classes 13.1 Procedural Programming! Data is stored in variables CS 2308 Spring 2015 Jill Seaman - Perhaps using arrays and structs.! Program is a collection of functions that perform

More information

Constructor - example

Constructor - example Constructors A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class name. The constructor is invoked whenever

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 10 October 1, 2018 CPSC 427, Lecture 10, October 1, 2018 1/20 Brackets Example (continued from lecture 8) Stack class Brackets class Main

More information

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year Object Oriented Programming Assistant Lecture Omar Al Khayat 2 nd Year Syllabus Overview of C++ Program Principles of object oriented programming including classes Introduction to Object-Oriented Paradigm:Structures

More information

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND Introduction to Classes 13.2 The Class Unit 4 Chapter 13 CS 2308 Spring 2017 Jill Seaman 1 l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains

More information

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator C++ Addendum: Inheritance of Special Member Functions Constructors Destructor Construction and Destruction Order Assignment Operator What s s Not Inherited? The following methods are not inherited: Constructors

More information

12/2/2009. The plan. References. References vs. pointers. Reference parameters. const and references. HW7 is out; new PM due date Finish last lecture

12/2/2009. The plan. References. References vs. pointers. Reference parameters. const and references. HW7 is out; new PM due date Finish last lecture The plan 11/30 C++ intro 12/2 C++ intro 12/4 12/7 12/9 12/11 Final prep, evaluations 12/15 Final HW7 is out; new PM due date Finish last lecture David Notkin Autumn 2009 CSE303 Lecture 25 CSE303 Au09 2

More information

Appendix G: Writing Managed C++ Code for the.net Framework

Appendix G: Writing Managed C++ Code for the.net Framework Appendix G: Writing Managed C++ Code for the.net Framework What Is.NET?.NET is a powerful object-oriented computing platform designed by Microsoft. In addition to providing traditional software development

More information

PIC 10A. Lecture 15: User Defined Classes

PIC 10A. Lecture 15: User Defined Classes PIC 10A Lecture 15: User Defined Classes Intro to classes We have already had some practice with classes. Employee Time Point Line Recall that a class is like a souped up variable that can store data,

More information

Constructors for classes

Constructors for classes Constructors for Comp Sci 1570 Introduction to C++ Outline 1 2 3 4 5 6 7 C++ supports several basic ways to initialize i n t nvalue ; // d e c l a r e but not d e f i n e nvalue = 5 ; // a s s i g n i

More information

Computer Programming with C++ (21)

Computer Programming with C++ (21) Computer Programming with C++ (21) Zhang, Xinyu Department of Computer Science and Engineering, Ewha Womans University, Seoul, Korea zhangxy@ewha.ac.kr Classes (III) Chapter 9.7 Chapter 10 Outline Destructors

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Lecture 3: Introduction to C++ (Continue) Examples using declarations that eliminate the need to repeat the std:: prefix 1 Examples using namespace std; enables a program to use

More information

Object-Oriented Design (OOD) and C++

Object-Oriented Design (OOD) and C++ Chapter 2 Object-Oriented Design (OOD) and C++ At a Glance Instructor s Manual Table of Contents Chapter Overview Chapter Objectives Instructor Notes Quick Quizzes Discussion Questions Projects to Assign

More information

Assignment of Structs

Assignment of Structs Deep Copy 1 Assignment of Structs 2 Slides 1. Table of Contents 2. Assignment of Structs 3. Dynamic Content 4. Shallow Copying 5. Assignment Operator 6. Deep Assignment Copy 7. Assignment Problems 8. Assignment

More information

Topics. Functions. Functions

Topics. Functions. Functions Topics Notes #8 Functions Chapter 6 1) How can we break up a program into smaller sections? 2) How can we pass information to and from functions? 3) Where can we put functions in our code? CMPT 125/128

More information

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver. Classes and Objects Week 5 Gaddis:.2-.12 14.3-14.4 CS 5301 Fall 2014 Jill Seaman The Class A class in C++ is similar to a structure. A class contains members: - variables AND - functions (often called

More information

COMP 2355 Introduction to Systems Programming

COMP 2355 Introduction to Systems Programming COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1 Today Class syntax, Constructors, Destructors Static methods Inheritance, Abstract

More information

CS304 Object Oriented Programming Final Term

CS304 Object Oriented Programming Final Term 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing

More information

Class and Function Templates

Class and Function Templates Class and Function 1 Motivation for One Way to Look at... Example: Queue of some type Foo C++ What can a parameter be used for? Instantiating a Template Usage of Compiler view of templates... Implementing

More information

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers and Arrays CS 201 This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers Powerful but difficult to master Used to simulate pass-by-reference

More information