C++ Programming
A Hands-On Lab Tutorial
Created by Dr. Bashar Alrjoub
February 17, 2025
Welcome to this introductory C++ programming tutorial. This guide covers fundamental programming concepts through practical examples and hands-on exercises.
Data Types in C++
C++ Data Type Categories:
Primitive Types
int
- Integerschar
- Charactersfloat
- Floating pointdouble
- Double precision
Derived Types
arrays
- Collectionspointers
- Memory addressesfunctions
- Code blocks
User-Defined
class
- Custom typesstructure
- Data groupingunion
- Shared memoryenum
- Constants
Working with ASCII Values
#include <iostream>
using namespace std;
int main() {
// Character variables
char letter1 = 'd'; // Direct character
char letter2 = 100; // ASCII value
// Display both character and ASCII values
cout << "letter1 (char): " << letter1 << endl;
cout << "letter1 (ASCII): " << (int)letter1 << endl;
cout << "letter2 (char): " << letter2 << endl;
cout << "letter2 (ASCII): " << (int)letter2 << endl;
return 0;
}
Output:
letter1 (char): d
letter1 (ASCII): 100
letter2 (char): d
letter2 (ASCII): 100
Simple Data Type Examples
#include <iostream>
using namespace std;
int main() {
// Integer type
int age = 20;
// Floating-point type
float height = 1.75;
// Character type
char grade = 'A';
// Double type
double pi = 3.14159;
// Display values and sizes
cout << "Age: " << age << " (Size: " << sizeof(age) << " bytes)" << endl;
cout << "Height: " << height << " (Size: " << sizeof(height) << " bytes)" << endl;
cout << "Grade: " << grade << " (Size: " << sizeof(grade) << " byte)" << endl;
cout << "Pi: " << pi << " (Size: " << sizeof(pi) << " bytes)" << endl;
return 0;
}
Output:
Age: 20 (Size: 4 bytes)
Height: 1.75 (Size: 4 bytes)
Grade: A (Size: 1 byte)
Pi: 3.14159 (Size: 8 bytes)
Temperature Converter Example
#include <iostream>
using namespace std;
int main() {
// Declare variables
int celsius; // Integer for Celsius temperature
float fahrenheit; // Float for Fahrenheit temperature
// Get temperature in Celsius
cout << "Enter temperature in Celsius: ";
cin >> celsius;
// Convert to Fahrenheit
fahrenheit = (celsius * 9.0/5.0) + 32;
// Display results
cout << celsius << " degrees Celsius = ";
cout << fahrenheit << " degrees Fahrenheit" << endl;
return 0;
}
Example Output:
Enter temperature in Celsius: 25
25 degrees Celsius = 77 degrees Fahrenheit
Student Information Example
#include <iostream>
using namespace std;
int main() {
// Declare variables of different types
char section = 'A'; // Character for class section
int studentId = 12345; // Integer for student ID
float gpa = 3.75; // Float for GPA
bool isActive = true; // Boolean for student status
// Display student information
cout << "Student Information:" << endl;
cout << "Section: " << section << endl;
cout << "ID: " << studentId << endl;
cout << "GPA: " << gpa << endl;
cout << "Active Student: " << (isActive ? "Yes" : "No") << endl;
return 0;
}
Additional Practice Exercises
- Write a program that:
- Declares variables for a student's quiz scores (integer)
- Calculates the average score (float)
- Shows the difference between integer and float division
- Create a program that:
- Stores a letter grade as a char
- Converts it to its ASCII value
- Shows if it's a passing grade (boolean)
Practice Exercise
Write a program that:
- Declares variables using different data types
- Assigns values to these variables
- Displays both the values and their sizes using sizeof()
- Converts a character to its ASCII value and back
📚 Additional Study Materials
- Week 1 (S1, E1) ECT 124
Introduction to fundamental C++ programming concepts including variable declarations, basic data types, and memory allocation. The module covers the implementation of primitive data structures, understanding of memory management principles, and proper syntax for variable initialization. Additionally, it encompasses the setup and configuration of the development environment, compiler installation, and basic command line operations for program compilation and execution.