Intro to C++ And Some Tools 9-2-2003. Opening Discussion zHave any questions come up since last class? Have you had a chance to look over the project.

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.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Road Map Introduction to object oriented programming. Classes
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
Guide To UNIX Using Linux Third Edition
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
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.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
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.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
String Processing Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have questions.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Hank Childs, University of Oregon May 13th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
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.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
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:
C/C++ Basics. Basic Concepts Basic functions of each language: Input, output, math, decision, repetition Types of errors: Syntax errors, logic errors,
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Constructors & Garbage Collection Ch. 9 – Head First 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 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
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.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
More Inheritance and More Java Stuff Opening Discussion zWhat did we talk about last class? zHave you read the description of assignment #2?
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Classes, Arrays & Pointers. Compiler & Linker expectations file1.cppfile2.cppfilen.cpp …. file1.ofile2.ofilen.o …. Linker application (executable) Compiler.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
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.
1 Introduction to Object Oriented Programming Chapter 10.
Memory Management in Java Mr. Gerb Computer Science 4.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Memory Management.
Hank Childs, University of Oregon
Learning Objectives Pointers as dada members
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Array Lists Chapter 6 Section 6.1 to 6.3
understanding memory usage by a c++ program
Java Programming Language
COP 3330 Object-oriented Programming in C++
Pointers and References
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

Intro to C++ And Some Tools

Opening Discussion zHave any questions come up since last class? Have you had a chance to look over the project description? If so do you have any other thoughts on it? zI simplified some of the submission details on Saturday. Basically I need your submission to have a makefile and two directories, one with Java code and one with C++ code. What is in those directories is up to you.

I/O in C++ zWe saw last time that standard input and output in C++ use the std::cin and std::cout objects. These are streams and can be found in the iostream library. zThey have overloaded the bit shifting operators ( >) to move data to and from streams. zThere are also ifstreams and ofstreams that can do I/O with files. zThe C libraries still work and we will use those for direct access binary files.

Memory in C++ zObjects can be on the stack in C++ which is ideal if possible. “Automatically managed.” zLike Java you will use the new operator to get dynamic memory in Java (don’t use malloc as you did in C). zUnlike Java, C++ has no garbage collection so you must call delete on everything you get with new. zFor arrays you use pointer=new type[#]; and delete[] pointer;. No shortcut for multidimensional arrays.

Class Basics zLike Java, C++ is a class based OOPL. zIt also provides data hiding with public, private, and protected visibility options. zMethods and members are grouped so you don’t specify the visibility for each elements. zOther flags have to be put on each elements: static, const, virtual, etc.

Constructors zAs with Java, when a new object is created a constructor for it is called. This happens either with new or when a stack variable of the type is declared. zConstructors have the same name as the class and can be overloaded with different arguments. (All functions/methods can be given default values for arguments in C++.) zFor greater efficiency you can also use initializer lists.

Destructors zBecause C++ lacks garbage collection, classes also have a destructor. This is a method that has a ‘~’ in front of the class name. It takes no arguments. zThis method is called any time delete is called on an object, or when a stack object passes out of scope. It should free any memory the object allocated and do other related cleanup.

Deep Copy vs. Shallow Copy zA shallow copy doesn’t create new copies of things that are pointed to. A deep copy does. In Java “object assignment” really copied a reference so this wasn’t really an issue. In C++ anything passed or returned by value does a copy so you have to think about this. zIf a class has pointers in it you typically need to include methods to handle deep copying.

Copy Constructors zOne of the methods you need to write for this is a copy constructor. Like in Java this is a constructor that takes a reference to an object of that type. zAnytime you pass an object by reference the copy constructor of that object will be called. zBy default C++ creates a copy constructor that does a shallow copy.

Overloading Assignment Operators zWe’ll talk about operator overloading in more general terms later, but it is possible to C++ to make almost every operator do something special for a given type. This includes the assignment operator. zHere again if a class includes pointers you will want to overload this operator. By default C++ gives you one that does a shallow copy.

Header Files zIn Java everything related to a class went into a file that bore the name of that class. In C++ the file names don’t really matter. However, you typically declare a class in a header file (ends with.h) and define the methods for it in a source file (ends with.cpp). zTo include one of your header files you put double quotes around the file name at the #include.

Make and Makefiles zWe use header files so we can do partial compilation. We compile different.cpp files to object files (.o) then link the object files to create executables. Make is a tool that helps with this. zMake is very powerful, but we only need the basics. You define various rules (often a filename created with that rule), say what files that rule depends on, and then say what happens when the rule is executed. A rule is only executed if the dependencies are newer than the specified file.

Installing Cygwin zIf you install Cygwin on your machine make sure to include the development packages. Those include make, gcc, and g++ compilers. zYou can run the Cygwin install multiple times and select different packages each time (or unselect ones you don’t need to get back disk space).

Using Eclipse zIf you run Eclipse normally under Windows it won’t find make or g++. That’s fine for Java development but will cause problems for C/C++. zTo fix this, run Eclipse from inside Cygwin. You can get to your normal Windows directory structure by going to /cygdrive. If you put Eclipse in Program Files then go to /cygdrive/c/Program\ Files/eclipse/ and you can run eclipse.exe. zNeed to install CDE for Eclipse and have a makefile in the project directory with “all” rule.

Minute Essay zWrite C++ code to allocate an array of num ints input them and print them in reverse order. zI will be leaving town right after class and won’t get back until late Friday. My e- mail access during that time will be sketchy. zStart working a bit on assignment #1 even without a partner. At least do design and think about approach.