C++ Programming for Non-C Programmers. Supplement

Size: px
Start display at page:

Download "C++ Programming for Non-C Programmers. Supplement"

Transcription

1 C++ Programming for Non-C Programmers Supplement

2 ii C++ Programming for Non-C Programmers C++ Programming for Non-C Programmers Published by itcourseware, E. Dry Creek Rd., Suite 150, Englewood, CO Contributing Authors: Jeff Howell, Danielle Waleri Editor: Danielle Waleri Special thanks to: Many instructors whose ideas and careful review have contributed to the quality of this workbook, including Evans Nash, and the many students who have offered comments, suggestions, criticisms, and insights. Copyright 1998 by itcourseware, Inc. All rights reserved. No part of this book may be reproduced or utilized in any form or by any means, electronic or mechanical, including photocopying, recording, or by an information storage retrieval system, without permission in writing from the publisher. Inquiries should be addressed to itcourseware, Inc., E. Dry Creek Rd., Suite 150, Englewood, Colorado, (303) All brand names, product names, trademarks, and registered trademarks are the property of their respective owners.

3 Table of Contents iii Contents Chapter 1 - Course Introduction... 7 Course Objectives... 9 Course Overview Chapter 2 - Getting Started Chapter Objectives The First Program (hello.cpp) Compile Steps How to Compile and Run a Program Exercises Chapter 3 - Data Types and Variables Chapter Objectives Fundamental Data Types Data Type Values and Sizes Data Type Values and Sizes Variable Declarations Variable names Constants Character Constants String Constants Exercises Chapter 4 - Operators and Expressions Chapter Objectives What are expressions? Arithmetic Operators Relational Operators Assignment Operator Expressions have resulting values True and False Logical Operators Increment and Decrement Operators (++ and --) Increment and Decrement Operators: Examples 'Operate-Assign' Operators (+=, *=,...) Conditional Expression Operator Precedence Precedence and order of evaluation... 75

4 iv C++ Programming for Non-C Programmers Evaluation of Logical Operators Type Conversions Type conversions (cont.) The cast operator Exercises Chapter 5 - Control Flow Chapter Objectives Statements if - else if() - else if() switch() Example: Switch() switch() (cont.) while() do - while() for() The for() loop - diagram Example: for() loop The break statement The continue statement Exercises Chapter 6 - Functions Chapter Objectives What is a function? Example: findbig3() Why use functions? Anatomy of a function Example: find_big_int() Arguments passed by value When to Use the return Statement Returning Non-integer Values Example: Returning Non-integer Values Functions in Multiple Source Files The Concept of Variable Scope Automatic Variables Global (external) variables Example: Global Variables Static Variables External Static Variables Exercises

5 Table of Contents v Chapter 7 - Pointers and Arrays Chapter Objectives What is a pointer? Pointer Operators Example: pointers Why use pointers? Example Arrays: Arrays (a picture) The & Operator Pointers and arrays Passing arrays to functions Initializing arrays Strings and character pointers What is char s[7]??? Arrays of pointers Arrays of pointers - diagram Command-line arguments Exercises Chapter 8 - Structures Chapter Objectives Comparison of structures and arrays Structure definitions Structure declarations Arrays of structures Exercises

6 vi C++ Programming for Non-C Programmers

7 Chapter 1 Course Introduction 7 Chapter 1 - Course Introduction

8 8 C++ Programming for Non-C Programmers Notes

9 Chapter 1 Course Introduction 9 Course Objectives Write applications using the C++ programming language. Use all of the basic syntax and semantics of the C and C++ languages. Write modular programs using functions. Use pointers and arrays to maintain data. Create classes that represent real-world objects with data and functionality. Inherit classes from existing classes to gain a reuse of functionality. Properly use references and constants to maintain data integrity and minimize errors. Write your own operators to work with the new data types that you create. Create template classes to avoid redundant coding and increase code reuse. Program object-oriented applications of moderate complexity.

10 10 C++ Programming for Non-C Programmers Notes

11 Chapter 1 Course Introduction 11 Course Overview Audience: This course is designed for programmers who want to move from procedural thinking to object-oriented programming using C++. Prerequisites: This course assumes that the student is an experienced professional programmer. Student Materials: Student workbook. A C++ text. Reference sheets. Classroom Environment: One terminal per student using the UNIX operating system, or a PC with a current C++ compiler.

12 12 C++ Programming for Non-C Programmers Notes

13 Chapter 2 Getting Started 13 Chapter 2 - Getting Started

14 14 C++ Programming for Non-C Programmers Notes

15 Chapter 2 Getting Started 15 Chapter Objectives Compile and run a program. Become familiar with your environment. Get "hands on" the machine immediately.

16 16 C++ Programming for Non-C Programmers Notes The following line: using namespace std ; is neccessary when including standard header files, like iostream. It will be discussed later in the course.

17 Chapter 2 Getting Started 17 The First Program (hello.cpp) /* hello.cpp */ #include <iostream> using namespace std ; int main(void) { cout << "hello, world" << endl; return 0; } /* int main(void) */

18 18 C++ Programming for Non-C Programmers Notes

19 Chapter 2 Getting Started 19 Compile Steps The source code file is the file that you create with an editor. The compiler compiles the source into an object file from which the linker creates an executable file. hello.cpp Source Code File hello.o Compile Object Code File hello Link Executable Binary File

20 20 C++ Programming for Non-C Programmers Notes

21 Chapter 2 Getting Started 21 How to Compile and Run a Program The linker needs to know the names of all of the object files that are part of your executable program. On a Unix platform, you create a makefile that contains the names of all of your files, then run the utility called make which compiles and links your code. In an Integrated Development Environment (IDE) such as Borland C++ or Microsoft Visual C++, you create a project that contains the names of all of your files, then running your program will first cause it to be compiled and linked.

22 22 C++ Programming for Non-C Programmers Notes

23 Chapter 2 Getting Started 23 Exercises 1. Edit hello.cpp such that it does not print the endl. Compile and run it. 2. Edit your solution so that it uses a second cout statement to print the endl. 3. Finally, have it print hello followed by your name.

24 24 C++ Programming for Non-C Programmers Notes

25 Chapter 6 Functions 119 Chapter 6 - Functions

26 120 C++ Programming for Non-C Programmers Notes

27 Chapter 6 Functions 121 Chapter Objectives Understand the use of functions in program structure. Write modular programs consisting of functions. Pass data to functions by value and by reference. "Build" a program from multiple source files. Initialize variables at definition time. Understand the scope of variables. Know when to use global variables.

28 122 C++ Programming for Non-C Programmers Notes

29 Chapter 6 Functions 123 What is a function? A function is a set of instructions. A function may operate on data passed to it. A program "calls" a function, passing it data, if required. A function can be called with different data and from different places. A function can "return" data to its caller.

30 124 C++ Programming for Non-C Programmers Notes

31 Chapter 6 Functions 125 Example: findbig3() /* findbig3.cpp */ #include <iostream> using namespace std ; int find_big_3(int, int, int); int main(void) { int x,y,z; int largest; x = 10; y = 15; z = 12; largest = find_big_3(x, y, z); cout << "Largest: " << largest << endl; return 0; } /* int main(void) */ /* --- Function find_big_3 --- */ int find_big_3(int a, int b, int c) { int big; big = a > b? a : b; /* Bigger of a and b */ big = big > c? big : c; /* Bigger of big and c */ return big; } /* int find_big_3(int a, int b, int c) */

32 126 C++ Programming for Non-C Programmers Notes

33 Chapter 6 Functions 127 Why use functions? To design a program into modules. To perform the same instructions on different sets of data. To create a library of reusable tools.

34 128 C++ Programming for Non-C Programmers Notes

35 Chapter 6 Functions 129 Anatomy of a function void main(void) { int arg1; float arg2; char arg3; func(arg1, arg2, arg3); /* Three arguments passed */ func(100, , 'X'); /* Constants as arguments */ } /* void main(void) */ /* Function returns a float, takes three parameters */ float func(int p1, float p2, char p3) /* Arguments in calls to func agree with */ /* parameters in number and type. */ /* They need not be of same name. */ {... define any needed local variables... operate on parameters... calculate a result (the function s "return value") return 3.14F; }/* float func(int p1, float p2, char p3) */

36 130 C++ Programming for Non-C Programmers Notes

37 Chapter 6 Functions 131 Example: find_big_int() /* findbigi.cpp */ #include <iostream> using namespace std ; int find_big_int(int [], int); int main(void) { int a[5]; a[0] = 985; a[1] = 255; a[2] = 868; a[3] = 1105;; a[4] = 499; cout << "Biggest: " << find_big_int(a, 5) << endl; return 0; } /* int main(void) */ /* --- Function find_big_int --- */ int find_big_int(int a[], int num_elem) /* int a[] - Tells compiler that a is an array */ /* int num_elem - Size of array passed in */ { int i; /* Automatic local variables */ int big; big = a[0]; /* To start, assume first element is biggest */ cout << big << endl; for (i = 1; i < num_elem; i++) if (a[i] > big) { /* If i'th element is bigger, */ big = a[i]; /* then update big holder */ cout << big << endl; } return big; } /* int find_big_int(int a[], int num_elem) */

38 132 C++ Programming for Non-C Programmers Notes

39 Chapter 6 Functions 133 Arguments passed by value The data passed to a function are copies of the caller's actual data. Functions can use passed in variables, but can't change them. /* passval.cpp */ #include <iostream> using namespace std ; int passval(int); int main(void) { int x; x=99; cout << "x before call: " << x << endl; passval(x); cout << "x after call: " << x << endl; return 0; } /* int main(void) */ /* --- Function passval --- */ int passval(int a) { a = a * 10; return a; } /* int passval(int a) */ Program will print: x before call: 99 x after call: 99

40 134 C++ Programming for Non-C Programmers Notes

41 Chapter 6 Functions 135 When to Use the return Statement Typically, use return when the function calculates and returns a single value. Use pass-by-reference when multiple values will be returned. In the second case, maybe you need more than one function.

42 136 C++ Programming for Non-C Programmers Notes

43 Chapter 6 Functions 137 Returning Non-integer Values By default, functions are assumed to return type int. If return value type is non-int, there must be two declarations: 1. The function type must be declared. 2. The calling function must declare the called function (usually in a prototype).

44 138 C++ Programming for Non-C Programmers Notes

45 Chapter 6 Functions 139 Example: Returning Non-integer Values /* max3.cpp */ #include <iostream> using namespace std ; int main(void) { float one, two,three, big, max3(float, float, float); cout << "Enter three float values: "; cin >> one >> two >> three; big = max3(one, two, three); cout << "Big = " << big << endl; return 0; } /* int main(void) */ float max3(float a, float b, float c) { float big; big = a > b? a : b ; big = big > c? big : c ; return big; } /* float max3(float a, float b, float c) */

46 140 C++ Programming for Non-C Programmers Notes

47 Chapter 6 Functions 141 Functions in Multiple Source Files Functions can reside in separate source files. They are compiled separately then linked together to form the executable program: On Unix: CC main.cpp func1.cpp func2.cpp -o progname In an IDE: Add main.cpp, func1.cpp and func2.cpp to your project. Each file is compiled separately and automatically linked together to create the executable.

48 142 C++ Programming for Non-C Programmers Notes

49 Chapter 6 Functions 143 The Concept of Variable Scope Variables are "written to" and "read from". The scope of a variable is all the functions in a program that can access that variable. Variables can be automatic (local), global, static or extern.

50 144 C++ Programming for Non-C Programmers Notes

51 Chapter 6 Functions 145 Automatic Variables Are local to a function (including main()). Are accessible only to the function in which they are defined. Are created "automatically" each time the function is called. Memory locations that store automatic variables are deallocated upon function exit.

52 146 C++ Programming for Non-C Programmers Notes

53 Chapter 6 Functions 147 Global (external) variables Are defined outside of any function. Are available to all functions, even those compiled separately (by using the extern keyword). Provide an alternative to function arguments and return values for communicating between functions. Local variables with same name as global will prevail.

54 148 C++ Programming for Non-C Programmers Notes

55 Chapter 6 Functions 149 Example: Global Variables /* gcount.cpp */ #include <iostream> using namespace std ; void f(void); int count; /* Global variable */ int main(void) { count = 0; f(); /* Once */ f(); /* Twice */ f(); /* Thrice */ cout << "f called " << count << " times" << endl; return 0; } /* int main(void) */ void f(void) { count++; } /* void f(void) */

56 150 C++ Programming for Non-C Programmers Notes

57 Chapter 6 Functions 151 Static Variables Two uses for static (We'll see the second in a minute). Within a function, static variables retain their values "for next call" when a function exits. /* statv.cpp */ #include <iostream> using namespace std ; void statfunc(int); int main(void) { statfunc(10); statfunc(20); return 0; } /* int main(void) */ void statfunc(int i) { static hold = 0; cout << "Previous hold: " << hold << endl; hold = i; cout << "Current hold: " << hold << endl; } /* void statfunc(int i) */

58 152 C++ Programming for Non-C Programmers Notes

59 Chapter 6 Functions 153 External Static Variables External (defined outside any function) static variables are only accessible within that source file. Useful for preventing name conflicts between separately compiled modules. Because... identically named external statics in different files are different, not the same (huh?).

60 154 C++ Programming for Non-C Programmers Notes

61 Chapter 6 Functions 155 Exercises 1. Write a program that reads four numbers from the keyboard and prints out the minimum, maximum, and average of the numbers. Define four separate float variables for the numbers. BE SURE to modularize the program into separate tasks that input the data, perform the calculations, and report the results. Do you think that an array version of this program would be better? 2. Starting with swmenu.cpp (from the previous chapter), create a menu-driven user interface for a program. Loop till the user chooses exit, and call a function for each switched choice. For now, just confirm to the user that the program entered the function. 3. Split the program from Exercise 1 into separately compilable source files, then compile and run the executable. 4. Write a simple program that calls a function which adds two floats and returns the sum.

62 156 C++ Programming for Non-C Programmers Notes

C++ Programming for Non-C Programmers. Supplement

C++ Programming for Non-C Programmers. Supplement C++ Programming for Non-C Programmers Supplement C++ Programming for Non-C Programmers C++ Programming for Non-C Programmers Published by ITCourseware, 7245 S. Havana St, Suite 100, Centennial, CO 80112

More information

CS2141 Software Development using C/C++ C++ Basics

CS2141 Software Development using C/C++ C++ Basics CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short

More information

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. 1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. B. Outputs to the console a floating point number f1 in scientific format

More information

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF

More information

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Introduction to Programming using C++

Introduction to Programming using C++ Introduction to Programming using C++ Lecture One: Getting Started Carl Gwilliam gwilliam@hep.ph.liv.ac.uk http://hep.ph.liv.ac.uk/~gwilliam/cppcourse Course Prerequisites What you should already know

More information

Scientific Computing

Scientific Computing Scientific Computing Martin Lotz School of Mathematics The University of Manchester Lecture 1, September 22, 2014 Outline Course Overview Programming Basics The C++ Programming Language Outline Course

More information

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Computer Science II Lecture 1 Introduction and Background

Computer Science II Lecture 1 Introduction and Background Computer Science II Lecture 1 Introduction and Background Discussion of Syllabus Instructor, TAs, office hours Course web site, http://www.cs.rpi.edu/courses/fall04/cs2, will be up soon Course emphasis,

More information

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement Last Time Let s all Repeat Together 10/3/05 CS150 Introduction to Computer Science 1 1 We covered o Counter and sentinel controlled loops o Formatting output Today we will o Type casting o Top-down, stepwise

More information

Name SECTION: 12:45 2:20. True or False (12 Points)

Name SECTION: 12:45 2:20. True or False (12 Points) Name SECION: 12:45 2:20 rue or False (12 Points) 1. (12 pts) Circle for true and F for false: F a) Local identifiers have name precedence over global identifiers of the same name. F b) Local variables

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program 1 By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program variables. Apply C++ syntax rules to declare variables, initialize

More information

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes Distributed Real-Time Control Systems Lecture 17 C++ Programming Intro to C++ Objects and Classes 1 Bibliography Classical References Covers C++ 11 2 What is C++? A computer language with object oriented

More information

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver) Introduction to C++ 1. General C++ is an Object oriented extension of C which was derived from B (BCPL) Developed by Bjarne Stroustrup (AT&T Bell Labs) in early 1980 s 2. A Simple C++ Program A C++ program

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CHAPTER 4 FUNCTIONS. 4.1 Introduction CHAPTER 4 FUNCTIONS 4.1 Introduction Functions are the building blocks of C++ programs. Functions are also the executable segments in a program. The starting point for the execution of a program is main

More information

CS2141 Software Development using C/C++ Compiling a C++ Program

CS2141 Software Development using C/C++ Compiling a C++ Program CS2141 Software Development using C/C++ Compiling a C++ Program g++ g++ is the GNU C++ compiler. A program in a file called hello.cpp: #include using namespace std; int main( ) { cout

More information

Chapter 2 Basic Elements of C++

Chapter 2 Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion

More information

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3 Programming - 1 Computer Science Department 011COMP-3 لغة البرمجة 1 011 عال- 3 لطالب كلية الحاسب اآللي ونظم المعلومات 1 1.1 Machine Language A computer programming language which has binary instructions

More information

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 1 INTRODUCTION

Chapter 1 INTRODUCTION Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware consists of the physical components of the system. The software is the collection of programs that a computer

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single Functions in C++ Problem-Solving Procedure With Modular Design: Program development steps: Analyze the problem Develop a solution Code the solution Test/Debug the program C ++ Function Definition: A module

More information

Lab 1: First Steps in C++ - Eclipse

Lab 1: First Steps in C++ - Eclipse Lab 1: First Steps in C++ - Eclipse Step Zero: Select workspace 1. Upon launching eclipse, we are ask to chose a workspace: 2. We select a new workspace directory (e.g., C:\Courses ): 3. We accept the

More information

W3101: Programming Languages C++ Ramana Isukapalli

W3101: Programming Languages C++ Ramana Isukapalli Lecture-6 Operator overloading Namespaces Standard template library vector List Map Set Casting in C++ Operator Overloading Operator overloading On two objects of the same class, can we perform typical

More information

Dr. Md. Humayun Kabir CSE Department, BUET

Dr. Md. Humayun Kabir CSE Department, BUET C++ Dr. Md. Humayun Kabir CSE Department, BUET History of C++ Invented by Bjarne Stroustrup at Bell Lab in 1979 Initially known as C with Classes Classes and Basic Inheritance The name was changed to C++

More information

CS 376b Computer Vision

CS 376b Computer Vision CS 376b Computer Vision 09 / 25 / 2014 Instructor: Michael Eckmann Today s Topics Questions? / Comments? Enhancing images / masks Cross correlation Convolution C++ Cross-correlation Cross-correlation involves

More information

Programming. C++ Basics

Programming. C++ Basics Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++

More information

A First Program - Greeting.cpp

A First Program - Greeting.cpp C++ Basics A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout

More information

Objectives. In this chapter, you will:

Objectives. In this chapter, you will: Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

More information

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++ DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++ Objective: To Learn Basic input, output, and procedural part of C++. C++ Object-orientated programming language

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below: QUIZ 1. Explain the meaning of the angle brackets in the declaration of v below: This is a template, used for generic programming! QUIZ 2. Why is the vector class called a container? 3. Explain how the

More information

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004 GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004 Functions and Program Structure Today we will be learning about functions. You should already have an idea of their uses. Cout

More information

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR 603203 DEPARTMENT OF COMPUTER SCIENCE & APPLICATIONS QUESTION BANK (2017-2018) Course / Branch : M.Sc CST Semester / Year : EVEN / II Subject Name

More information

Why C++? C vs. C Design goals of C++ C vs. C++ - 2

Why C++? C vs. C Design goals of C++ C vs. C++ - 2 Why C++? C vs. C++ - 1 Popular and relevant (used in nearly every application domain): end-user applications (Word, Excel, PowerPoint, Photoshop, Acrobat, Quicken, games) operating systems (Windows 9x,

More information

C++ for Python Programmers

C++ for Python Programmers C++ for Python Programmers Adapted from a document by Rich Enbody & Bill Punch of Michigan State University Purpose of this document This document is a brief introduction to C++ for Python programmers

More information

Chapter 1 Introduction to Computers and C++ Programming

Chapter 1 Introduction to Computers and C++ Programming Chapter 1 Introduction to Computers and C++ Programming 1 Outline 1.1 Introduction 1.2 What is a Computer? 1.3 Computer Organization 1.7 History of C and C++ 1.14 Basics of a Typical C++ Environment 1.20

More information

Introduction to Algorithms and Programming (COMP151)

Introduction to Algorithms and Programming (COMP151) Introduction to Algorithms and Programming (COMP151) A Student's Manual for Practice Exercises Dr. Mohamed Aissa m.issa@unizwa.edu.om 11i13 Summer 2014 Practice Exercises #1 Introduction Page 2 Practice

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

About Codefrux While the current trends around the world are based on the internet, mobile and its applications, we try to make the most out of it. As for us, we are a well established IT professionals

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College October 24, 2018 Outline Outline 1 Chapter 8: A C++ Introduction For Python Programmers Expressions and Operator Precedence

More information

CS201- Introduction to Programming Current Quizzes

CS201- Introduction to Programming Current Quizzes CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements

More information

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is

More information

Topics. Functions. Functions

Topics. Functions. Functions Topics Notes #8 Functions Chapter 6 1) How can we break up a program into smaller sections? 2) How can we pass information to and from functions? 3) Where can we put functions in our code? CMPT 125/128

More information

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016 Chapter 6: User-Defined Functions Objectives In this chapter, you will: Learn about standard (predefined) functions Learn about user-defined functions Examine value-returning functions Construct and use

More information

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points) Name rue or False (12 Points) 1. (12 pts) Circle for true and F for false: F a) Local identifiers have name precedence over global identifiers of the same name. F b) Local variables retain their value

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 2. Overview of C++ Prof. Amr Goneid, AUC 1 Overview of C++ Prof. Amr Goneid, AUC 2 Overview of C++ Historical C++ Basics Some Library

More information

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays Outline 13.1 Test-Driving the Salary Survey Application 13.2 Introducing Arrays 13.3 Declaring and Initializing Arrays 13.4 Constructing

More information

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

Fundamentals of Programming CS-110. Lecture 2

Fundamentals of Programming CS-110. Lecture 2 Fundamentals of Programming CS-110 Lecture 2 Last Lab // Example program #include using namespace std; int main() { cout

More information

Tutorial-2a: First steps with C++ programming

Tutorial-2a: First steps with C++ programming Programming for Scientists Tutorial 2a 1 / 18 HTTP://WWW.HEP.LU.SE/COURSES/MNXB01 Introduction to Programming and Computing for Scientists Tutorial-2a: First steps with C++ programming Programming for

More information

Exercise: Inventing Language

Exercise: Inventing Language Memory Computers get their powerful flexibility from the ability to store and retrieve data Data is stored in main memory, also known as Random Access Memory (RAM) Exercise: Inventing Language Get a separate

More information

COMP322 - Introduction to C++ Lecture 01 - Introduction

COMP322 - Introduction to C++ Lecture 01 - Introduction COMP322 - Introduction to C++ Lecture 01 - Introduction Robert D. Vincent School of Computer Science 6 January 2010 What this course is Crash course in C++ Only 14 lectures Single-credit course What this

More information

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program: Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

More information

1 Unit 8 'for' Loops

1 Unit 8 'for' Loops 1 Unit 8 'for' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment

More information

AN OVERVIEW OF C++ 1

AN OVERVIEW OF C++ 1 AN OVERVIEW OF C++ 1 OBJECTIVES Introduction What is object-oriented programming? Two versions of C++ C++ console I/O C++ comments Classes: A first look Some differences between C and C++ Introducing function

More information

BITG 1233: Introduction to C++

BITG 1233: Introduction to C++ BITG 1233: Introduction to C++ 1 Learning Outcomes At the end of this lecture, you should be able to: Identify basic structure of C++ program (pg 3) Describe the concepts of : Character set. (pg 11) Token

More information

Introduction to C++ Systems Programming

Introduction to C++ Systems Programming Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions

More information

Introduction to C ++

Introduction to C ++ Introduction to C ++ Thomas Branch tcb06@ic.ac.uk Imperial College Software Society October 18, 2012 1 / 48 Buy Software Soc. s Free Membership at https://www.imperialcollegeunion.org/shop/ club-society-project-products/software-products/436/

More information

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1 Lab 2: Pointers 1. Goals Further understanding of pointer variables Passing parameters to functions by address (pointers) and by references Creating and using dynamic arrays Combing pointers, structures

More information

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++ Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++ 0.1 Introduction This is a session to familiarize working with the Visual Studio development environment. It

More information

WARM UP LESSONS BARE BASICS

WARM UP LESSONS BARE BASICS WARM UP LESSONS BARE BASICS CONTENTS Common primitive data types for variables... 2 About standard input / output... 2 More on standard output in C standard... 3 Practice Exercise... 6 About Math Expressions

More information

OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING LAB 1 REVIEW THE STRUCTURE OF A C/C++ PROGRAM. TESTING PROGRAMMING SKILLS. COMPARISON BETWEEN PROCEDURAL PROGRAMMING AND OBJECT ORIENTED PROGRAMMING Course basics The Object

More information

Chapter 15 - C++ As A "Better C"

Chapter 15 - C++ As A Better C Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

C++ (Non for C Programmer) (BT307) 40 Hours

C++ (Non for C Programmer) (BT307) 40 Hours C++ (Non for C Programmer) (BT307) 40 Hours Overview C++ is undoubtedly one of the most widely used programming language for implementing object-oriented systems. The C++ language is based on the popular

More information

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program: Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

More information

CS242 COMPUTER PROGRAMMING

CS242 COMPUTER PROGRAMMING CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing

More information

University of Toronto

University of Toronto University of Toronto Faculty of Applied Science and Engineering Midterm November, 2010 ECE244 --- Programming Fundamentals Examiners: Tarek Abdelrahman, Michael Gentili, and Michael Stumm Instructions:

More information

Unit 6. Thinking with Functions

Unit 6. Thinking with Functions 1 Unit 6 Thinking with Functions Function Decomposition 2 Functions Overview Functions (aka procedures, subroutines, or methods) are the unit of code decomposition and abstraction Map Service ValidateInputs()

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Chapter 6 Example Activity Diagram 1 Outline Chapter 6 Topics 6.6 C++ Standard Library Header Files 6.14 Inline Functions 6.16 Default Arguments 6.17 Unary Scope Resolution Operator

More information

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. The basic commands that a computer performs are input (get data), output (display result),

More information

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (! FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. Assume that all variables are properly declared. The following for loop executes 20 times.

More information

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University (5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University Key Concepts 2 Object-Oriented Design Object-Oriented Programming

More information

Input And Output of C++

Input And Output of C++ Input And Output of C++ Input And Output of C++ Seperating Lines of Output New lines in output Recall: "\n" "newline" A second method: object endl Examples: cout

More information

Crash Course into. Prof. Dr. Renato Pajarola

Crash Course into. Prof. Dr. Renato Pajarola Crash Course into Prof. Dr. Renato Pajarola These slides may not be copied or distributed without explicit permission by all original copyright holders C Language Low-level programming language General

More information

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program Overview - General Data Types - Categories of Words - The Three S s - Define Before Use - End of Statement - My First Program a description of data, defining a set of valid values and operations List of

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

Computer Programming : C++

Computer Programming : C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program

More information

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING THROUGH C++ Practical: OOPS THROUGH C++ Subject Code: 1618407 PROGRAM NO.1 Programming exercise on executing a Basic C++

More information

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object CHAPTER 1 Introduction to Computers and Programming 1 1.1 Why Program? 1 1.2 Computer Systems: Hardware and Software 2 1.3 Programs and Programming Languages 8 1.4 What is a Program Made of? 14 1.5 Input,

More information

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage: Discussion 1H Notes (Week 3, April 14) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 More on Arithmetic Expressions The following two are equivalent:! x = x + 5;

More information

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8 Pointers Variables vs. Pointers: A variable in a program is something with a name and a value that can vary. The way the compiler and linker handles this is that it assigns a specific block of memory within

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Lecture 3: Introduction to C++ (Continue) Examples using declarations that eliminate the need to repeat the std:: prefix 1 Examples using namespace std; enables a program to use

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010 CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)

More information

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. CMPSC11 Final (Study Guide) Fall 11 Prof Hartman Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) This is a collection of statements that performs

More information

Lecture 2: C Programming Basic

Lecture 2: C Programming Basic ECE342 Introduction to Embedded Systems Lecture 2: C Programming Basic Ying Tang Electrical and Computer Engineering Rowan University 1 Facts about C C was developed in 1972 in order to write the UNIX

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011 The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science Instructor: Final Exam Fall 2011 Last Name :... ID:... First Name:... Section No.: EXAMINATION

More information

Data Structures using OOP C++ Lecture 3

Data Structures using OOP C++ Lecture 3 References: th 1. E Balagurusamy, Object Oriented Programming with C++, 4 edition, McGraw-Hill 2008. 2. Robert L. Kruse and Alexander J. Ryba, Data Structures and Program Design in C++, Prentice-Hall 2000.

More information

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C Sample Test Paper-I Marks : 25 Time:1 Hrs. Q1. Attempt any THREE 09 Marks a) State four relational operators with meaning. b) State the use of break statement. c) What is constant? Give any two examples.

More information

Lab # 02. Basic Elements of C++ _ Part1

Lab # 02. Basic Elements of C++ _ Part1 Lab # 02 Basic Elements of C++ _ Part1 Lab Objectives: After performing this lab, the students should be able to: Become familiar with the basic components of a C++ program, including functions, special

More information