CSCE 3110 Data Structures & Algorithms C++ warm-up.

Slides:



Advertisements
Similar presentations
Outline zHistory zWhat is C++ zHow does C++ relate to other OO languages zTypes of applications for C++ zComponents of MSs Visual C zAdvantages /
Advertisements

Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
C++ fundamentals.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming. Table of Contents History What is C++? Development of C++ Standardized C++ What are the features of C++? What is Object Orientation?
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Outline  History  What is C++  How does C++ relate to other OO languages  Types of applications for C++  Components of MS’s Visual C  Advantages.
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.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Copyright Curt Hill Variables What are they? Why do we need them?
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CSC 212 Object-Oriented Programming and Java Part 2.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
The Machine Model Memory
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Java Primer 1: Types, Classes and Operators
Programming Languages and Paradigms
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
Programming Paradigms
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Constructors and Destructors
CPS120: Introduction to Computer Science
Object Oriented Programming in java
Java Programming Language
Programming Languages and Paradigms
Presentation transcript:

CSCE 3110 Data Structures & Algorithms C++ warm-up

C++ Warm-up History What is C++ How does C++ relate to other OO languages Types of applications for C++ Advantages / Disadvantages of C++

History of C : C language developed at Bell Labs Dennis Ritchie wrote C for Unix OS Needed C for work with Unix late 70s: C becomes popular for OS development by many vendors Many variants of the language developed ANSI standard C in

History of C++ (continued) early 80s: Bjarne Stroustrup adds OO features to C creating C++ 90s: continued evolution of the language and its applications preferred language for OS and low level programming popular language for application development low level control and high level power

Conceptually what is C++ Alternatives: is it C, with lots more options and features? is it an OO programming language with C as its core? is it a development environment? On most systems it is a development environment, language, and library, used for both procedural and object oriented programming, that can be customized and extended as desired

Versions of C++ ANSI C++ Microsoft C++ (MS Visual C++ 6.0) Other vendors: Borland, Symantec, Turbo, … Many older versions (almost annual) including different version of C too Many vendor specific versions Many platform specific versions For this class: Unix / Linux based versions g++

Characteristics of C++ as a Computer Language Procedural Object Oriented Extensible...

Other OO Languages Smalltalk pure OO language developed at PARC Java built on C/C++ objects and data types Eifel and others

What you can do with C++ Apps (standalone, Web apps, components) Active desktop (Dynamic HTML, incl Web) Create graphical apps Data access ( , files, ODBC) Integrate components w/ other languages

Disadvantages of C++ Tends to be one of the less portable languages Complicated? 40 operators, intricate precedence, pointers, etc. can control everything many exceptions and special cases tremendous libraries both standard, vendor specific, and available for purchase, but all are intricate Aspects above can result in high maintenance costs

Advantages of C++ Available on most machines Can get good performance Can get small size Can manage memory effectively Can control everything Good supply of programmers Suitable for almost any type of program (from systems programs to applications)

Basics of C/C++: Topics to Cover Basic types Operators Flow control Classes Inheritance

Primitive Types booltrue or false (only C++) char8/16-bit short 16-bit signed integer int32-bit signed integer unsigned32-bit unsigned integer long32 / 64-bit signed integer float32-bit floating point double64-bit floating point

Operators and Precedence []. to access arrays elements / to access object methods and fields expr++ expr-- ++expr --expr ! new (type)expr * / % + - > (integers only) >= <= == != &

Operators and Precedence ^ | && (booleans only) || (booleans only) ?: = += -= *= …. C++ allows operator overloading

Precedence Example What is: / 4 % 3 = 5 + (21 / 4) % 3 = 5 + ( 5 % 3) = = 7

Explicit Casting (type) expression Possible among all integer and float types Possible among some class references E.g. int i = (int) ( (double)5 / (double)3 )

Implicit Casting Applied automatically provided there is no loss of precision float  double int  double Example int iresult, i=3; double dresult, d=3.2; dresult = i/d => implicit casting dresult= iresult = i/d => error! Why? Loss in precision, needs explicit casting

Control Flow if (boolean) statement; else if(boolean) statement2; else statement3; Booleans only, not integers! if (i > 0)  legal if (i = 2)  legal / illegal ?

Switch / case switch (controlVar) { case 'a' : statement-1 break; case 'b' : statement-2 break; default : statement-3 break; } Do not forget the break command to avoid surprise result!

Loops while( ) statement; do statement; while( ) for(init-expr; ; incr-expr) statement;

Loop Refresher Which loops must execute their statements at least once? Which loops can choose to never execute their statements? Which value of the boolean indicates to do the statements again?

Value vs. Reference Variables Variables of primitive types are value variables Variables of arrays or classes are reference variables 10 i int i =10; j 10 int * j = new int[1]; j[0] = 10; new performs dynamic memory allocation

Passing Parameters All parameters in C/Java are passed “by value” C++ allows for three different ways of passing parameters: Pass “by value” E.g. foo (int n) Appropriate for small objects (usually primitive types) that should not be altered by the function call Pass “by constant reference” E.g. foo(const T& myT) Appropriate for large objects that should not be altered by the function call Pass “by reference” E.g. foo(bool & errFlag) Appropriate for small objects that can be altered by the function call Array types are always passed “by reference”

Arrays Refer to several values of the same type Create new array int* myArray = new int[20]; Index from 0 to length-1 Delete an array delete [] myArray;

Classes in C++: Why Create Classes / Objects? Keeps all related info (i.e., data) together Refer to all the related info by one name Protect the information Hide methods that use or change the info Keep methods together with their related info

Example of Benefits of Creating an Object Keeps all related info (i.e., data) together Person thisPerson; Person thisPerson = new Person ("Bill", "Clinton", 52, Person.MALE); Refer to all the related info by one name thisPerson Protect the information lastName = "Dole"; //normally data members are private, and member functions are public

Example of a Simple Class class Change { private: int quarters; int dimes; public: int getQuarters() {return quarters;} int getDimes() {return dimes;} void setQuarters(int aQuarters) {quarters = aQuarters;} …... void printChange() {cout << "\nQuarters: " << quarters << " Dimes: " << dimes << endl; } };

Instantiating an Object The class definition does not create any objects Instantiating and constructing are equivalent words for building a new object based on the model (i.e., template) of the class Instantiating is done just like declaring a variable of a built in data type Instantiating is done by a constructor (sometimes called a constructor method) If the "class provider" does not provide a constructor, then the C++ compiler provides a default one automatically The default constructor does not provide values to the data members (i.e. the instance variables)

Instantiating an Object (more) When the object is instantiated, memory is allocated Example of instantiation (implicit call of constructor) Car myCar; Elephant oneElephant, twoElephant; No initialization takes place Each object has its own memory allocation oneElephant and twoElephant are separate objects in different locations in memory Each is addressed individually by name or location Each data member is addressed individually using the object name and the data member name, for example: oneElephant.age twoElephant.name

Referencing an Object Each object has a name (or a location) which is assigned when the object is instantiated private data members are accessible only within the class since most data members are private, that means that these data items are accessed generally by means of member functions myElephant.age = 72; //won't work, assuming age //is declared as private myElephant.setAge(72); // will work

Defining the Member Function Outside the Class int main() { …. } //end of main function //DEFINE MEMBER FUNCTION OUTSIDE THE CLASS void Change::addChange(Change myChange) //global scope resolution { quarters += myChange.quarters; //can refer to data members directly dimes += myChange.dimes; pennies += myChange.pennies; } …. //another member function definition here

Invoking or Calling the Member Function //MAIN int main(void) { //declare two Change objects Change firstChange, secondChange;//constructs the objects firstChange.setQuarters(2);//calls member function for object secondChange.setDimes(8);//calls member function for different object firstChange.printChange(); firstChange.addChange(secondChange); //add the amounts together cout << "amounts added together: "; //print out firstChange firstChange.printChange(); } //end of main

Inheritance The power of object-oriented languages Enables reuse of fields/methods All parent fields included in child instantiation Protected and public fields and methods directly accessible to child Parent methods may be overridden New fields and methods may be added to the child Multiple inheritance

Inheritance (cont’d) class classname: public parentname { private: ….; public: ….; //access to parent methods through // parentname::methodname … }

Finally: Some Conventions for Variable Names Use letters and numbers Do not use special characters including spaces, dots, underlines, pound signs, etc. The first letter will be lower case Use variable names that are meaningful (except for occasional counters that we might call i, j, x, etc.) You can concatenate words, and capitalize each after the first, e.g., bankBal, thisAcctNum, totAmt If you abbreviate, be consistent. For example do not use both bankBal and totalBalance as variable names.

Some Conventions for Struct and Class Names In creating names of structs and classes, apply the same rules as for variable names, except the first character will be upper case Example: an object's name: myCar the struct or class name: Car Another Example: aPerson and Person