You are on page 1of 13

Introduction to

programming C++

Introduction to Programming C++


_____________________________
Course Summary
1
This course covers the fundamental concepts
of programming using the C++ language.
• The course starts with an introduction to variables and data
types, including integers, floating-point numbers, and
characters. It then covers arrays, which allow you to store
multiple values of the same type in a single variable.
• Next, the course covers control structures, including for loops

Introduction to Programming C++


and while loops, which allow you to repeat a block of code
multiple times.
• We also introduced pointers and explain their use cases.
• The course then delves into functions, which allow you to
modularize your code and reuse it in different parts of your
program. Finally, the course covers classes and objects, which
provide a way to encapsulate data and functions into a single 2
unit.
Key Topics

• Variables and data types


• Arrays
• Control structures
• if conditional statements
• for loops

Introduction to Programming C++


• while loops
• Switch case
• Functions
• Classes and objects

3
Variables and data types
• In programming, variables are used to store and
manipulate data.
• Data types define the type of data that can be stored in a
variable, such as integer, float, or string.

Introduction to Programming C++


• Understanding variables and data types is crucial for
writing programs that can perform calculations, store data,
and produce outputs.

• Pay attention to: variable types and what they can store,
memory usage
4
Arrays
• An array is a collection of data items of the same type that are stored
in contiguous memory locations.
• Using arrays, you can store and manipulate large amounts of data
efficiently.
• Arrays are used extensively in programming, and understanding how

Introduction to Programming C++


to use them is essential for developing programs that can handle
complex data structures.

• Pay attention to: how to create and initialize an array, how to access
specific items in the array, printing the contents, etc.

5
Control structures: if conditional
statements, for loops, while loops:
1. Control structures are used to control the flow of a program.
2. If conditional statements are used to execute certain code only if a
specified condition is met.
3. Loops are used to execute a block of code repeatedly, either for a fixed
number of times (for loops) or until a condition is met (while loops).

Introduction to Programming C++


4. These structures are the building blocks of many programs and
understanding how to use them is essential for developing efficient
and effective programs

5. Pay attention to: code structure, how to implement each structure and
when to use them.

6
Switch case
• The switch statement provides a way to execute different code blocks
based on the value of a variable or an expression. It allows you to
write concise and readable code that performs different actions based
on specific conditions.
• The switch statement is often used in conjunction with the if-else

Introduction to Programming C++


statement to control the flow of a program.

• Pay attention to: Use cases and purpore of this structure.

7
Functions
• Functions are blocks of code that perform a specific task.
• They allow you to break your program into smaller, more manageable pieces, making it
easier to develop, test, and debug.
• Functions can be called from other parts of the program and can return values, making
them extremely versatile and useful.

Introduction to Programming C++


• Pay attention to: how to implement a function, return type and return value. How to use
a function with parameter and without.

8
Classes and Objects
• In C++, a class is a blueprint for creating objects.
• An object is an instance of a class, and it contains data and functions that operate on that
data.
• Classes and objects are used extensively in C++ programming and are essential for
developing large-scale programs that can handle complex data structures and perform
advanced operations.

Introduction to Programming C++


• Understanding how to use classes and objects is a key skill for any C++ programmer.

• Pay attention to: Understanding what is class and what is object. Private methods and
private data members, how to implement a class and how constructor. How to create an
object (constructor) and use implemented public member function on it: ( . operator)

9
Sample tasks for implementing
in C++
1. Write a program that reads in a series of integers from the user,
and then prints the sum and average of those integers.
• Instructions: Use a while loop to prompt the user for input until they enter a negative number.
Use a counter variable to keep track of the number of integers entered, and a sum variable to
accumulate the total sum. Calculate the average by dividing the sum by the number of integers
entered.

Introduction to Programming C++


2. Write a program that reads in an array of integers and finds the
maximum and minimum values in the array.
• Instructions: Initialize two variables to hold the maximum and minimum values, and then use
a for loop to iterate over each element in the array, updating the maximum and minimum
values as necessary.
3. Write a program that prompts the user to enter an integer, and
then prints whether the number is even or odd.
• Instructions: Use the modulus operator to check whether the input number is divisible by 2. If
the remainder is 0, the number is even; otherwise, it is odd.

10
Sample tasks for implementing
in C++ (functions)
1. Create a C++ program that converts a temperature given in Celsius
to Fahrenheit. Prompt the user to enter the temperature in
Celsius, and then output the temperature in Fahrenheit. Use the
formula F = C * (9.0/5.0) + 32.0 to perform the conversion.

Introduction to Programming C++


2. Create a C++ function that returns the absolute value of a number.
Prompt the user to enter a number, and then output the absolute
value of that number.
3. Create a C++ function that returns the factorial of a positive integer.
Prompt the user to enter a positive integer, and then output the
factorial of that integer.
4. Create a C++ function that returns the smallest element in an array
of integers. Prompt the user to enter the size of the array and its
11
elements, and then output the smallest element.
Sample tasks for implementing
in C++ (class and object)
1. Create a class called "Calculator" with private member variables for two numbers.
Implement public functions called "add", "subtract", "multiply", and "divide" that perform
the corresponding mathematical operations on the two numbers. To use this class, create
a Calculator object, set its two numbers using public member functions, and then call the
appropriate math function to get the calculated result.

2. Create a class called "Person" with private member variables for name and age.

Introduction to Programming C++


Implement a public function called "introduce" that prints out a message introducing the
person by name and age. To use this class, create a Person object, set its name and age
using public member functions, and then call the "introduce" function to print out the
introduction.

3. Create a class called "Student" with private member variables for name, ID number, and
GPA. Implement public functions called "printName", "printID", and "printGPA" that print
out the corresponding information for a given student object. To use this class, create a
Student object, set its name, ID number, and GPA using public member functions, and
then call the appropriate print function to output the information to the screen.
12
Note: Practice both ways of setting values: through a constructor and setter function as in
rectangle example covered during last class.
Conclusions
By mastering these topics, you now have a solid foundation in the basics of programming
using C++.
You have learned how to write simple programs and understand more advanced
programming concepts in the future.
Keep practicing and honing your skills, and you will be well on your way to becoming a
proficient C++ programmer.

Introduction to Programming C++


13

You might also like