C++ Notes

C vs. C++

All C library headers with .h remove the .h and prepend c:

C C++
#include <stdio.h> #include <cstdio>
#include <stdlib.h> #include <cstdlib>
#include <string.h> #include <cstring>
#include <ctype.h> #include <cctype>
#include <stddef.h> #include <cstddef>

Namespaces

Namespaces in C++ provide a mechanism to group identifiers (like classes, objects, and functions) under a name. This helps avoid name conflicts in larger projects.

Declaring a Namespace

namespace MyNamespace {
    int x, y;
};

To access values within a namespace, use the scope operator ::.

MyNamespace::x = 3; // Assigns 3 to x in MyNamespace

To avoid repeatedly specifying a namespace, you can use the using namespace directive:

using namespace MyNamespace;

Note: The std namespace is commonly used for the C++ Standard Library.

Working with Strings

Library to Include

Getting String Length

In C, string length is obtained using strlen:

char *AString; // Points to a C string
strlen(AString); // Returns length of string

In C++, you use the length() method:

std::string AString;
AString.length(); // Returns length of string

Copying Strings

In C, copying strings involves manual memory allocation and using strcpy:

char Original[] = "Some string";
char *Copy;
// Allocate space equal to string length plus null terminator
Copy = malloc(strlen(Original) + 1);
strcpy(Copy, Original);

In C++, string copying is simpler:

std::string Original = "Some string";
std::string Copy = Original;

Comparing Strings

In C, use strcmp to compare strings:

char *A, *B;
if (strcmp(A, B) == 0) {
    // strcmp returns 0 if A == B
} else if (strcmp(A, B) < 0) {
    // strcmp returns < 0 if A < B
}

In C++, you can use the == and < operators:

std::string A, B;
if (A == B) {
    // do something if A == B
} else if (A < B) {
    // do something if A < B
}

Concatenating Strings

In C, string concatenation requires manual allocation and use of strcat:

char *A, *B, *C;
// Allocate space for concatenated string
C = malloc(strlen(A) + strlen(B) + 1);
strcpy(C, A);
strcat(C, B);

In C++, use the + operator:

std::string A, B, C;
C = A + B;

Input/Output (I/O)

Library to Include

Standard Output

In C, output is handled with printf:

int I = 3;
double D = 2.2;
char S[] = "Hello";
printf("I = %d\n", I);
printf("D = %lf\n", D);
printf("S = %s\n", S);

In C++, use std::cout:

int I = 3;
double D = 2.2;
std::string S = "Hello";
std::cout << "I = " << I << std::endl;
std::cout << "D = " << D << std::endl;
std::cout << "S = " << S << std::endl;

Standard Input

In C, input is often handled with scanf and fgets:

int I;
double D;
char S[128]; // Can only accept strings of 127 chars
scanf("%d%lf", &I, &D);
fgets(S, sizeof(S), stdin); // Safer than scanf

In C++, use std::cin:

int I;
double D;
std::string S;
std::cin >> I >> D >> S;

File Output

In C, file output is done using fopen and fprintf:

FILE *OutFile = fopen("out.txt", "w");
fprintf(OutFile, "This is a file!\n");
fclose(OutFile);

In C++, use std::ofstream:

std::ofstream OutFile("out.txt");
OutFile << "This is a file!" << std::endl;

File Input

In C, use fopen and fscanf:

FILE *InFile = fopen("in.txt", "r");
int I;
double D;
fscanf(InFile, "%d%lf", &I, &D);

In C++, use std::ifstream:

std::ifstream InFile("in.txt");
int I;
double D;
InFile >> I >> D;

File Seeking

In C, use fseek and ftell:

FILE *InFile = fopen("in.txt", "r");
int FileLength;
fseek(InFile, 0, SEEK_END);
FileLength = ftell(InFile);
fseek(InFile, 0, SEEK_SET);

In C++, use seekg and tellg:

std::ifstream InFile("in.txt");
InFile.seekg(0, InFile.end);
int FileLength = InFile.tellg();
InFile.seekg(0, InFile.beg);

Exercises

Write your solutions to these problems on this class whiteboard.

  1. Convert the following C code using printf to C++ using std::cout:
    int num = 10;
    double pi = 3.14;
    printf("Number: %d, Pi: %lf\n", num, pi);
  2. Write a C++ program that concatenates two strings and outputs the result.
  3. Write a program that checks if a string is a palindrome (the same forward and backward) using C++ strings.
  4. Write a C++ program to:
  5. Create a C++ program that:

Exam-Style Questions

  1. Describe how namespaces help in avoiding name conflicts in C++.
  2. Explain the process of copying a string in C, detailing the importance of memory allocation.
  3. Compare and contrast the methods of string comparison in C and C++.
  4. Discuss the differences in handling file I/O between C and C++.