Professional Documents
Culture Documents
programming C++
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.
• 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
• 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).
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
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.
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.
• 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.
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.
2. Create a class called "Person" with private member variables for name and age.
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.