Classes, Arrays & Pointers. Compiler & Linker expectations file1.cppfile2.cppfilen.cpp …. file1.ofile2.ofilen.o …. Linker application (executable) Compiler.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
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.
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.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
Reviews for Exam 1 Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, Fall 2010.
Reviews for Exam 1 Chapter 1-4 CS 211 Data Structures MHC, 2007.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
C++ data types. Structs vs. Classes C++ Classes.
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
C++ vs. Java: Similiarities & Differences Dr. Jeyakesavan Veerasamy Director of CS UTDesign program & CS Teaching Faculty University.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
Java and C++, The Difference An introduction Unit - 00.
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Game Programming in Java Dr. Jeyakesavan Veerasamy CS faculty, The University of Texas at Dallas Website:
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
FASTFAST All rights reserved © MEP Make programming fun again.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Learning Objectives Pointers as dada members
BRIEF Overview ON THE MAJOR Similarities & Differences
By Muhammad Waris Zargar
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Programming with ANSI C ++
Class Operations Pointer and References with class types
Class: Special Topics Copy Constructors Static members Friends this
Chapter 1-4 CSc 212 Data Structures, Sec AB CCNY, Spring 2012
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Andy Wang Object Oriented Programming in C++ COP 3330
BRIEF Overview ON THE MAJOR Similarities & Differences
Review Chapter 10 PPT for full coverage.
Andy Wang Object Oriented Programming in C++ COP 3330
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
C++ data types.
Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, 2009
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

Classes, Arrays & Pointers

Compiler & Linker expectations file1.cppfile2.cppfilen.cpp …. file1.ofile2.ofilen.o …. Linker application (executable) Compiler C++ compiler does not care about filenames.

Classes High level concepts are same as Java – details are bit different. Example: Complex Numbers Unlike Java, – C++ compiler does not care about filenames. – C++ uses 2 files for each class: one header (.h) file for definition, another source file (.cpp) for implementation – Need to use “#include ….h” to use any class.

Concepts: Constructor Constructor – mostly similar to Java – Exception: default values for formal parameters

Concepts: Operator overloading Operator overloading: ComplexNumber x, y, z; z = x + y; In Java?

Concepts: Pass by value or reference Passing parms by value or reference User selection const keyword for reference types Java? Method overloading – similar to Java – use different argument types to differentiate

Concepts: Method overloading Method overloading – similar to Java – use different argument types to differentiate

Concepts: Friend friend designation - breaks OOP philosophy! – specific functions/methods outside the class can access private data  Why?

Concepts: Objects Objects can be created as local variables just like any basic data types. ComplexNumber num1;

Arrays Basic data types and classes are treated the same way in C++, unlike Java. ComplexNumber numbers[5];

Array version #2 ComplexNumber *numbers; numbers = new ComplexNumber[5];

Array version #3 (equivalent to Java) ComplexNumber **numbers; numbers = new ComplexNumber*[5]; for( index i = 0 ; i < 5 ; i++) numbers[i] = new ComplexNumber(…);

Pointers Explicit in C++: ComplexType *cnump; Pointer arithmetic: (cnump + 5) “Address of” operator: & Dereference operator for objects: -> cnump->setComplex(…); Dynamic memory allocation requires pointers (just like references in Java)

Dynamic memory allocation No automatic garbage collection in C++ # of new invocations should match # of delete invocations. If a class constructor allocates memory (i.e. uses “new …”), it needs a destructor method too – it should use “delete …” to release allocated memory.