0% found this document useful (0 votes)
12 views7 pages

C++ Programming Basics Guide

This document serves as an introductory guide to C++, covering essential topics such as setup, syntax, data types, functions, object-oriented programming, and file handling. It includes practical examples and exercises to reinforce learning. The material aims to provide a solid foundation for beginners to start programming in C++.

Uploaded by

Joshua Huang
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

C++ Programming Basics Guide

This document serves as an introductory guide to C++, covering essential topics such as setup, syntax, data types, functions, object-oriented programming, and file handling. It includes practical examples and exercises to reinforce learning. The material aims to provide a solid foundation for beginners to start programming in C++.

Uploaded by

Joshua Huang
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++ Learning Material

This document provides an introduction to C++, a powerful, general-purpose


programming language known for its performance and flexibility. It covers the
basics of C++ programming, including setup, syntax, data types, functions,
object-oriented programming, and file handling, with practical examples.

Table of Contents
1. What is C++?
2. Setting Up C++
3. Basic Syntax and Structure
4. Data Types and Variables
5. Functions
6. Object-Oriented Programming
7. File Handling
8. Common C++ Libraries
9. Practice Exercises

What is C++?
C++ is a high-performance, compiled programming language that extends C with
features like object-oriented programming, templates, and the Standard Template
Library (STL). It is widely used in system programming, game development,
and high-performance applications.

Setting Up C++
To start coding in C++:
1. Install a Compiler:
• Windows: Use MinGW or MSVC (Microsoft Visual Studio).
• macOS: Use Xcode or install g++ via Homebrew.
• Linux: Install g++ (e.g., sudo apt install g++ on Ubuntu).
2. Install an IDE: Popular choices include Visual Studio Code, CLion, or
Code::Blocks.
3. Verify Installation: Run the following in a terminal to check the compiler
version:
g++ --version
1. Write and Compile: Save your code with a .cpp extension and com-
pile/run using:
g++ [Link] -o hello
./hello # On Windows: [Link]

1
Basic Syntax and Structure
A basic C++ program has the following structure:
#include <iostream>
int main() {
std::cout << "Hello, C++!" << std::endl;
return 0;
}
• #include <iostream>: Includes the input/output stream library.
• int main(): The entry point of the program.
• std::cout: Outputs text to the console.
• std::endl: Adds a newline and flushes the output buffer.
Save this as [Link], compile, and run it.

Data Types and Variables


C++ is statically typed, requiring explicit type declarations.

Basic Data Types


• Integers: int, long, short.
• Floating-point: float, double.
• Characters: char.
• Strings: std::string (requires #include <string>).
• Booleans: bool.

Variable Declaration
#include <iostream>
#include <string>
int main() {
int age = 25;
double salary = 55000.50;
std::string name = "Alice";
bool isStudent = true;
std::cout << "Name: " << name << ", Age: " << age
<< ", Salary: " << salary << ", Student: " << isStudent << std::endl;
return 0;
}

Arrays and Vectors


• Array: Fixed-size collection.
• Vector: Dynamic array (requires #include <vector>).

2
#include <iostream>
#include <vector>
int main() {
int numbers[] = {1, 2, 3, 4, 5}; // Array
std::vector<int> vec = {10, 20, 30}; // Vector
vec.push_back(40); // Add element
for (int num : vec) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}

Functions
Functions in C++ are defined with a return type and can take parameters.

Example: Calculate Factorial


#include <iostream>
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
int main() {
int num = 5;
std::cout << "Factorial of " << num << " is " << factorial(num)
<< std::endl;
return 0;
}

Object-Oriented Programming
C++ supports object-oriented programming with classes and objects.

Example: Student Class


#include <iostream>
#include <string>
class Student {
public:
std::string name;
int id;
double grade;
Student(std::string n, int i, double g) : name(n), id(i), grade(g)
{}

3
void display() {
std::cout << "ID: " << id << ", Name: " << name
<< ", Grade: " << grade << std::endl;
}
};
int main() {
Student student("Bob", 101, 85.5);
[Link]();
return 0;
}
• class: Defines a blueprint for objects.
• public: Access specifier for class members.
• Constructor: Initializes objects.

File Handling
C++ supports file input/output using <fstream>.

Example: Write and Read File


#include <iostream>
#include <fstream>
#include <string>
int main() {
// Write to file
std::ofstream outFile("[Link]");
if (outFile.is_open()) {
outFile << "Hello, C++ File Handling!" << std::endl;
[Link]();
}
// Read from file
std::ifstream inFile("[Link]");
std::string line;
if (inFile.is_open()) {
while (std::getline(inFile, line)) {
std::cout << line << std::endl;
}
[Link]();
}
return 0;
}

Common C++ Libraries


• <iostream>: Input/output operations.
• <string>: String manipulation.

4
• <vector>: Dynamic arrays.
• <fstream>: File handling.
• <algorithm>: Common algorithms like sorting and searching.

Practice Exercises
1. Write a program to calculate the square of a number using a function.
2. Create a vector of 5 integers and print them in reverse order.
3. Define a Course class with attributes for course_id, name, and credits,
and a method to display details.
4. Write a program to read numbers from a file and calculate their sum.
5. Write a function to check if a string is a palindrome.

Example Solutions
Exercise 1: Square Function #include <iostream>
int square(int num) {
return num * num;
}
int main() {
int num = 6;
std::cout << "Square of " << num << " is " << square(num) <<
std::endl;
return 0;
}

Exercise 2: Reverse Vector #include <iostream>


#include <vector>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
for (int i = [Link]() - 1; i >= 0; i--) {
std::cout << numbers[i] << " ";
}
std::cout << std::endl;
return 0;
}

Exercise 3: Course Class #include <iostream>


#include <string>
class Course {
public:
int course_id;
std::string name;
int credits;
Course(int id, std::string n, int c) : course_id(id), name(n),
credits(c) {}

5
void display() {
std::cout << "Course ID: " << course_id << ", Name: " << name
<< ", Credits: " << credits << std::endl;
}
};
int main() {
Course course(101, "Mathematics", 3);
[Link]();
return 0;
}

Exercise 4: Sum Numbers from File #include <iostream>


#include <fstream>
int main() {
std::ifstream inFile("[Link]");
int num, sum = 0;
if (inFile.is_open()) {
while (inFile >> num) {
sum += num;
}
[Link]();
std::cout << "Sum: " << sum << std::endl;
}
return 0;
}

Exercise 5: Palindrome Check #include <iostream>


#include <string>
bool isPalindrome(std::string str) {
int left = 0, right = [Link]() - 1;
while (left < right) {
if (str[left] != str[right]) return false;
left++;
right--;
}
return true;
}
int main() {
std::string text = "radar";
std::cout << text << (isPalindrome(text) ? " is a palindrome" : "
is not a palindrome") << std::endl;
return 0;
}

6
Conclusion
This material provides a foundation for learning C++. Practice these examples
using a C++ compiler and IDE to build proficiency. For advanced topics, explore
the STL, templates, and multithreading with <thread>.

You might also like