Intro to C++
Computer Graphics and Imaging
UC Berkeley CS 184/284A
Discussion 1
Why C++?
C++ in CS 184/284A
What does this code do?
#include <iostream>
using namespace std;
int main()
{
cout << "Enter a positive integer: ";
int n;
cin >> n;
int k = 1;
for (int i = 1; i <= n; i++) {
k *= i;
}
cout << "The number " << n << " turns into " << k << endl;
return 0;
}
What does this code do?
#include <iostream>
using namespace std;
int main()
{
cout << "Enter a positive integer: ";
int n;
cin >> n;
int k = 1;
for (int i = 1; i <= n; i++) {
k *= i;
}
cout << "The number " << n << " turns into " << k << endl;
return 0;
}
Namespaces, Classes, and Objects
Namespaces
Namespaces Example
#include <iostream>
namespace hello {
int x = 5; // variable inside namespace
}
int main() {
int x = 123; // local variable
std::cout << x << std::endl; // prints “123”
std::cout << hello::x << std::endl;
// prints “5”
}
We can declare two x variables! One inside the namespace and one outside.
To refer to a namespace variable:
hello::variable
Namespaces Example
#include <iostream>
namespace n {
int x = 5;
}
namespace n2 {
int x = 4;
}
using namespace n;
int main() {
std::cout << x << std::endl; // Prints 5
}
Alternatively, use the using keyword.
Namespaces Example
#include <iostream>
namespace n {
int x = 5;
}
namespace n2 {
int x = 4;
}
using namespace n;
using namespace n2;
int main() {
std::cout << x << std::endl; // which x is it????
}
Alternatively, use the using keyword.
Careful: It could cause ambiguities if you’re using multiple namespaces with the same variable names.
Similar bad practice in Python:
from module_1 import *�from module_2 import *
Namespaces Example
#include <iostream>
namespace n {
int x = 5;
}
namespace n2 {
int x = 4;
}
using namespace n;
using namespace n2;
int main() {
std::cout << x << std::endl; // which x is it????
}
The compiler will tell you something’s wrong!
Classes & Objects
struct Student {
std::string student_name;
int age;
bool enrolled_in_cs184;
};
Classes and Objects
#include <iostream>
class Rectangle {
private:
int width;
int height; // private variables
public:
void set_values (int,int);
int area();
// functions to declare
};
void Rectangle::set_values(int x, int y) {
width = x;
height = y;
}
int Rectangle::area() {
return width * height;
}
int main() {
Rectangle rect;
rect.set_values (3,4);
std::cout << "Area: " << rect.area();
return 0;
}
// What is the output?
Classes and Objects
#include <iostream>
class Rectangle {
private:
int width;
int height; // private variables
public:
void set_values (int,int);
int area();
// functions to declare
};
void Rectangle::set_values(int x, int y) {
width = x;
height = y;
}
int Rectangle::area() {
return width * height;
}
int main() {
Rectangle rect;
rect.set_values (3,4);
std::cout << "Area: " << rect.area();
return 0;
}
// What is the output?
12
Header Files
.cpp vs. .h
// Rectangle.h:
class Rectangle {
private:
int width;
int height;
public:
void set_values(int,int);
int area();
};
// Rectangle.cpp:
#include “Rectangle.h”
void Rectangle::set_values(int x, int y) {
width = x;
height = y;
}
int Rectangle::area() {
return width * height;
}
// We write out the explicit code for the method in the .cpp file!
.cpp vs. .h
// Rectangle.h:
class Rectangle {
private:
int width;
int height;
public:
void set_values(int,int);
int area();
};
// main.cpp:
#include “Rectangle.h”
int main() {
Rectangle rect;
rect.set_values (3,4);
std::cout << "Area: " << rect.area();
return 0;
}
Memory Allocation
Different from Java but very similar to C!
Pointers and Addresses
Pointers and Addresses
Pointers and Addresses
int *x;
// pointer variable is named x
// stores the address of an int in memory
Reference & Dereference Operators
Reference & Dereference Operators
Pointers Example
// Example program
#include <iostream>
#include <string>
int main()
{
int *a;
int x = 3;
a = &x;
std::cout << a << std::endl;
std::cout << *a << std::endl;
*a = 100;
std::cout << *a << std::endl;
std::cout << x << std::endl;
}
Pointers Example
// Example program
#include <iostream>
#include <string>
int main()
{
int *a;
int x = 3;
a = &x;
std::cout << a << std::endl;
std::cout << *a << std::endl;
*a = 100;
std::cout << *a << std::endl;
std::cout << x << std::endl;
}
Pointers Example
// Example program
#include <iostream>
#include <string>
int main()
{
int *a;
int x = 3;
a = &x;
std::cout << a << std::endl;
std::cout << *a << std::endl;
*a = 100;
std::cout << *a << std::endl;
std::cout << x << std::endl;
}
Pointers Example
// Example program
#include <iostream>
#include <string>
int main()
{
int *a;
int x = 3;
a = &x;
std::cout << a << std::endl;
std::cout << *a << std::endl;
*a = 100;
std::cout << *a << std::endl;
std::cout << x << std::endl;
}
Pointers Example
// Example program
#include <iostream>
#include <string>
int main()
{
int *a;
int x = 3;
a = &x;
std::cout << a << std::endl;
std::cout << *a << std::endl;
*a = 100;
std::cout << *a << std::endl;
std::cout << x << std::endl;
}
Pointers Example
// Example program
#include <iostream>
#include <string>
int main()
{
int *a;
int x = 3;
a = &x;
std::cout << a << std::endl;
std::cout << *a << std::endl;
*a = 100;
std::cout << *a << std::endl;
std::cout << x << std::endl;
}
-> Operator
Rectangle *x = new Rectangle(3, 4);
int w = x->width;
// this is the same as (*x).width
Rectangle x = Rectangle(3, 4);
int w = x.width;
x.area();
-> Operator
Rectangle *x = new Rectangle(3, 4);
int w = x->width;
// this is the same as (*x).width
x->area();
// also used to call methods
Rectangle x = Rectangle(3, 4);
int w = x.width;
x.area();
Memory Management
Memory Management
Memory Management
Memory Management
Stack
Rectangle rect;
OR
Rectangle rect = Rectangle();
Rectangle rect(3, 4);
OR
Rectangle rect = Rectangle(3, 4);
Rectangle *rp = new Rectangle();
Rectangle *rp = new Rectangle(3, 4);
Heap
Passing Arguments
Pass By Value
int square_value(int a) {
return a * a;
}
int main() {
int x = 2;
x = square_value(x);
}
Pass By Value
int square_value(int a) {
return a * a;
}
int main() {
int x = 2;
x = square_value(x); // x is now 4!
}
Pass By Pointer
void square_pointer(int *a) {
*a = (*a) * (*a);
}
int main() {
int x = 2;
// Passing in an address
square_pointer(&x);
}
Pass By Pointer
void square_pointer(int *a) {
*a = (*a) * (*a);
}
int main() {
int x = 2;
// Passing in an address
square_pointer(&x); // After this line, x = 4
}
Pass By Reference
void square_reference(int &a) {
a = a * a;
}
int main() {
int x = 2;
square_reference(x); // After this line, x = 4
}
Pass By Pointer vs. Reference vs. Value
void square_pointer(int *a) {
*a = (*a) * (*a);
}
void square_reference(int &a) {
a = a * a;
}
int square_value(int a) {
return a * a;
}
int main() {
int x = 2;
square_pointer(&x); // After this line, x = 4
square_reference(x); // After this line, x = 16
x = square_value(x); // After this line, x = 256
}
Common Built-in Data Structure: vector!
Vectors
Vectors Example
int main() {
// We initialize a vector with an initializer list
std::vector<int> nums = {2, 4, 6, 0, 1};
// Access by index
std::cout << "nums[2] is " << nums[2] << std::endl;
// Index-based iteration
for (int i = 0; i < nums.size(); i++) {
std::cout << nums[i] << " "; // prints 2 4 6 0 1
}
// Range for loop
for (int x : nums) {
std::cout << x << " "; // prints 2 4 6 0 1
}
}
Range For Loops Warning
int main() {
// We initialize a vector with an initializer list
std::vector<Image> images = std::vector<Image>(5);
// Range for loop (items are copied here)
for (Image image : images) {
//do something
}
}
Range For Loops Warning
int main() {
// We initialize a vector with an initializer list
std::vector<Image> images = std::vector<Image>(5);
// Range for loop (over references)
for (Image &image : images) {
//do something
}
// Alternatively:
for (auto it = images.begin(); it != images.end(); ++it) {
//do something
}
}
Additional Resources
Thanks for coming!