Programming Languages and Paradigms Programming C++ Classes.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
C++ Classes & Data Abstraction
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
Enhancing classes Visibility modifiers and encapsulation revisited
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Java Programming Review (Part I) Enterprise Systems Programming.
OOP Languages: Java vs C++
Abstract classes and Interfaces. Abstract classes.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 12: Adding Functionality to Your Classes.
Introduction to Object-Oriented Programming
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Programming Languages and Paradigms Object-Oriented Programming.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Class Relationships and Object Interaction. 8/8/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Learners Support Publications Classes and Objects.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
Objects and Classes Mostafa Abdallah
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.
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 3 Introduction to Classes and Objects Definitions Examples.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Chapter 3 Implementing Classes
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Chapter 5: Enhancing Classes
Classes C++ representation of an object
2 Chapter Classes & Objects.
Lecture 3 John Woodward.
Programming Languages and Paradigms
Programming Paradigms
Java Programming Language
Classes and Objects.
Programming Languages and Paradigms
Classes CS 21a: Introduction to Computing I
Classes C++ representation of an object
Programming Languages and Paradigms
Classes and Objects Systems Programming.
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

Programming Languages and Paradigms Programming C++ Classes

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 2 Program Structure C++ Program: collection of files Files contain class, function, and global variable declarations/definitions main() function still the entry point just like in C

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 3 Header Files (.h) Contains class declarations Prototypes for functions outside of classes Others

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 4 Source Files (.cpp) Function/method definitions Directives (e.g., #include, #define) Variables and initialization (global/static variables)

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 5 Variables in C++ Regular variables int x; x = 5; Button b; Pointers int *p; p = & x; Button *q; q = new Button(); References/Aliases int & r = x; // initialization required Arrays int num[20]; int *a; a = new int[size];

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 6 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 static (class-level) fields are permitted Constructor/initialization is more involved (than in Java)

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 7 Field Initialization in C++ 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 ) …

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 8 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 9 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 10 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 11 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 12 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!

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 13 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 14 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?

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 15 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).