SEG4110 – Advanced Software Design and Reengineering TOPIC I Introduction to C++ For those who know Java And OO Principles in General.

Slides:



Advertisements
Similar presentations
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming.
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Lecture 9 Concepts of Programming Languages
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.
OOP Languages: Java vs C++
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Pointer Data Type and Pointer Variables
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Object-Oriented Programming in C++
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
 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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
More C++ Features True object initialisation
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
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.
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.
1.Which of the following statements is correct?
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
ISBN Object-Oriented Programming Chapter Chapter
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Classes, Interfaces and Packages
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Overview of C++ Polymorphism
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Pointers and Dynamic Arrays
Topic: Classes and Objects
Overloading Operator MySting Example
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Java Primer 1: Types, Classes and Operators
Class: Special Topics Copy Constructors Static members Friends this
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Java Programming Language
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Constructors and Destructors
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CISC/CMPE320 - Prof. McLeod
Overview of C++ Polymorphism
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
SPL – PS3 C++ Classes.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

SEG4110 – Advanced Software Design and Reengineering TOPIC I Introduction to C++ For those who know Java And OO Principles in General

SEG Topic I - Introduction to C++2 What aspects of C++ are similar to Java? Comments: // /* stuff */ Basic syntax such as: int I for(i=1; i<5; i++) { } class Classname { … ) C++ is strongly typed

SEG Topic I - Introduction to C++3 Some differences from Java Instance methods are generally called member functions -But the concept is the same Instance variables are generally called data members -But the concept is the same To declare a group of members private, public etc., precede the group by -private: -public:

SEG Topic I - Introduction to C++4 Example code for a file Student.h class Student { private: char *name; CourseOfferingList *registrations; public: //... maybe other stuff here too... char *name(void); void changeName(char *newName); void register(CourseOffering *newCourseOffering); void deregister (CourseOffering *oldCourseOffering); }

SEG Topic I - Introduction to C++5 Creating a derived class (subclass) from a base class (superclass) class GraduateStudent : public Student { private: Professor *supervisor public:... } The keyword ‘ public ’ on line 1 means the inherited public methods and variables are also to be public here It is bad design practice to use ‘ private ’ in this context -Causes inherited things to be private here

SEG Topic I - Introduction to C++6 Use ‘virtual’ to enable polymorphism class Student {... virtual int cgpa(void); } class GraduateStudent : public Student { // repeating virtual is optional virtual int cgpa(void); } class UndergradStudent : public student { virtual int cgpa(void); }

SEG Topic I - Introduction to C++7 To declare an abstract method class Student {... virtual int cgpa(void) = 0;... } The term ‘ pure virtual ’ is used instead of ‘ abstract ’

SEG Topic I - Introduction to C++8 Bodies of methods are declared separately from the declarations in.cpp files char *Student::name(void) { return name; } void Student::changeName(char *newName) { delete name;// get rid of old name name = new char[strlen(newName)+1]; strcpy(name, newName); }

SEG Topic I - Introduction to C++9 Variables can be… Pointers -As in C, you can have a pointer to a primitive int *c; -You can have a pointer to an object of a class Student *aStudent; Values (for both primitives and objects) int c; Student s; // the variable contains // the storage for the object References (pointers that are operated on like values) int &s; Student &s;

SEG Topic I - Introduction to C++10 Example using pointers vs references Standard C code for a swapping function using pointers swap(int *a, int *b) { int temp; temp = * a; (* a) = * b; (* b) = temp; } swap(&array[pos1],&array[pos2]);

SEG Topic I - Introduction to C++11 Example using pointers vs references, cont. Use references to avoid excessive dereferencing swap(int &a, int &b) { int temp; temp = a; a = b; b = temp } swap(array[pos1],array[pos2]);

SEG Topic I - Introduction to C++12 Calling a member function (method) If the variable student1 contains a pointer student1->register(courseOffering1); If student1 contains a reference or a value student1.register(courseOffering1);

SEG Topic I - Introduction to C++13 Creating and deleting objects Use ‘ new ’ as in Java to create an object Use ‘ delete ’ to free the memory when done -C++ does not have garbage collection by default -You have to define ‘ destructors ’ —Discussed later

SEG Topic I - Introduction to C++14 Declaring a default constructor in a.h file Actually if you omit code, an empty default constructor like this is class Student { Student(); }

SEG Topic I - Introduction to C++15 Defining the default constructor in the corresponding.cpp file Student::Student() // Default constructor to initialize an // empty student { static const defaultName = ” unknown ” ; name = new char[strlen(defaultName)+1]; strcpy(name, defaultName); registrations = new CourseOfferingList; year = 0; } Constructors with different arguments can be created

SEG Topic I - Introduction to C++16 You should declare and define a copy constructor in each class Used when Assigning an object to a newly defined value variable —The following are equivalent Student student2 = student1; Student student3(student1); Calling by value

SEG Topic I - Introduction to C++17 Defining a copy constructor Student::Student(const Student &original) // Constructor to copy a student { char *tempName; tempName = original.name(); name = new char[strlen(tempName)+1]; strcpy(name, tempName); // copy registrations registrations = new CourseOfferingList( original.registrations()); year = original.year; }

SEG Topic I - Introduction to C++18 You should declare and define an overloaded assignment operator Used when -Assigning an object to a existing value variable student2 = student1;

SEG Topic I - Introduction to C++19 Defining an overloaded assignment operator Student &Student::operator=( const Student &old) { delete name; delete registrations; // performs function of copy // constructor because we cannot call // the copy constructor directly copy(&old); return *this; }

SEG Topic I - Introduction to C++20 Additional syntactic oddities regarding constructors You can supply default arguments -When an object is created, not all arguments need to be supplied X::X(int, int=0) You can declare variables in two ways X x1(1); X x2 = 1;

SEG Topic I - Introduction to C++21 More object construction syntax Initialization to be performed before the body of a constructor is executed class X { int a, b, &c; } X::X(int i; int j): a(i), b(j), c(a) { } Is the same as X::X(int i; int j) { a=i; b=j; c=a; }

SEG Topic I - Introduction to C++22 Destructors class Student { ~Student(void); } in body (student.cpp): Student::~Student(void) // Student destructor { delete name; delete registrations; }

SEG Topic I - Introduction to C++23 Operator overloading NumHolder& NumHolder::operator++() { // update the instance variable ++value;... // Return reference to self return *this; } NumHolder NumHolder::operator+(NumHolder &other) { NumHolder sum; sum.value(value+other.value()); return sum; }

SEG Topic I - Introduction to C++24 Stream output int a;... // the following scans for an integer // and puts it in a cin >> a;... // the following prints a string then an // integer cout << “ the number is ” << a;

SEG Topic I - Introduction to C++25 Templates (generic programming) template class Stack { private: TYPE data[SIZE]; int top; public Stack() {top = 0;} void Push (const TYPE &c) {data[top++] = c;} void Pop(TYPE &c) {c = data[--top];} } Stack mystack; Stack anotherstack;