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.

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Classes. What is a class? Data and the functions (methods) that operate on that data – collectively called members –Example: bank account class Provide.
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.
Review of C++ Programming Part II Sheng-Fang Huang.
Java Programming Review (Part I) Enterprise Systems Programming.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 12: Adding Functionality to Your Classes.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Programming Languages and Paradigms Object-Oriented Programming.
Classes CS 21a: Introduction to Computing I First Semester,
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition 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.
Programming Languages and Paradigms Programming C++ Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
 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.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Learners Support Publications Constructors and Destructors.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Procedural and Object-Oriented Programming
Classes C++ representation of an object
By Muhammad Waris Zargar
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Andy Wang Object Oriented Programming in C++ COP 3330
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Introduction to Classes
Java Programming Language
Dr. Bhargavi Dept of CS CHRIST
Constructors and Destructors
Classes and Objects.
Classes CS 21a: Introduction to Computing I
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Final Exam Review Inheritance Template Functions and Classes
Corresponds with Chapter 5
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

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 contain class, function, and global variable declarations/definitions main() function serves as entry point for the executable

Header Files (.h) Contains class declarations Prototypes for functions outside of classes Other declarations

Source Files (.cpp) Function/method definitions Directives (e.g., #include) Variables and initialization (global and static variables)

Variables in C++ Regular variables int x; x = 5; BankAccount b; Pointers int *p; p = & x; BankAccount *q; q = new BankAccount; References/Aliases int & r = x; // initialization required Arrays int num[20]; int *a; a = new int[size]; BankAccount *many; many = new BankAccount[10];

Class Declaration class A { members … }; Members (fields and methods) grouped by public, private or protected regions Fields can be regular variables, arrays, pointers, or references Methods often defined outside of class declaration If defined inside declaration, it will be an inline method Convention: class declaration is in a.h file, method definitions are in a corresponding.cpp file

Constructors and destructors Constructor: special “method” used for object initialization Signature: same as class name, no return type May have several forms if overloaded (constructors with or without parameters) Called in many situations: variable declaration, new, array creation, … Destructor: special “method” used for object destruction Called when object variables go out of scope or during delete

C++ Destructor Special method whose signature is a ~ followed by the name of the class e.g., ~SomeClass(); Particularly if the class contains pointers and the constructor contains calls to new, a destructor needs to be defined e.g., SomeClass() { A = new int[20]; } ~SomeClass() { delete [] A; }

C++ Control Over Copy and Assignment In C++, the semantics of “a = b” (assignment) can be specified by defining the assignment operator In C++, there is a copy constructor specifies what happens during object copying, e.g., when function parameters are passed There is more low-level control shallow copy vs deep copy Both a copy constructor and an assignment operator should be defined if there are dynamically allocated portions in constructor

Field Initialization in C++ Fields: attributes/variables of a class Initialization cannot be performed during field declaration For non-reference variables, initialization can be carried out in the constructor body Static fields need to be initialized separately (outside the class declaration) as a global variable For, reference variables, use special constructor syntax: classname( type param ): fieldname( param ) …

Regular Initialization class BankAccount { private: int balance; Person *holder; public: BankAccount( int b, Person *p ) { balance = b; holder = p; } }; … Person john; BankAccount b( 1000, &john ); BankAccount object holder Person object

Comment on Object Fields class BankAccount { private: int balance; Person holder; public: BankAccount( int b, Person p ) { balance = b; holder = p; } }; … Person john; BankAccount b( 1000, john ); Creates a Person object for every BankAccount object; Probably not the intention Copy constructor, then assignment, is invoked BankAccount object holder Person object

Initializing References class BankAccount { private: int balance; Person& holder; public: BankAccount( int b, Person& p ) :holder( p ) { balance = b; } }; … Person john; BankAccount b( 1000, john ); holder is an alias to an external object; Existence of the object is enforced BankAccount object holder Person object john

Comparison Use regular variables for tight object composition contained object is created during object construction of the containing class Use pointers for associations Associated object is created externally and may be updated (e.g., void changeAccountHolder(Person *p)…) Use references for more permanent associations and to ensure the existence of the associated object upon construction

Back to Object Field Example class Car { private: Engine eng; public: Car( Engine e ) { eng = e; } }; … Engine newengine; Car c( newengine ); Constructor, copy constructor, then assignment, is invoked; 3 operations!

Better to use references even with object fields class Car { private: Engine eng; public: Car( Engine& e ): eng(e) {} }; … Engine newengine; Car c( newengine ); Only the copy constructor is invoked; eng is built from e, which is an alias for newengine

Inheritance and Constructors class Employee { … public: Employee() { } // this is called Employee( string & s ) { } // not this }; class Manager: public Employee { … public: Manager( string & s ) { } }; Manager’s constructor implicitly calls the default constructor of employee; How do we call a specific Employee constructor?

Inheritance and the Special Constructor Syntax class Employee { … public: Employee() { } Employee( string & s ) { } }; class Manager: public Employee { … public: Manager( string & s ): Employee( s ) { } }; Use the special syntax to call a particular superclass constructor (analogous to super() in Java).

Other important C++ features Virtual functions (for dynamic binding) Multiple inheritance (how to disambiguate between conflicting methods) Operator overloading (operators as methods) Templates (generics) Namespaces (managing identifiers) * See sample code on website