COP3503 Quiz 1
MyClass.cpp
#include "MyClass.h" MyClass::MyClass(int f, float ov) { this-> foo=f; otherValue=ov; } int MyClass::GetFoo() { return foo; }
#include brackets vs. quotes
#include <filename> ... search official directories, predefined by compiler #include "MyOwnHeader.h" ... search directory of file containing the directory (search locally)
anatomy of C++ app
#include <iostream> int main() { std::cout << "Hello, world!"; return 0; }
class basics
File: Person.h class Person { private: string name; int age; float salary; public: string nickname; Person (); int GetAge(); float GetSalary(); };
namespace
The set of variable and function names that have been reserved by the compiler/interpreter.
source files
definitions of those functions and classes (usually main.cpp and functions.cpp)
declaration
for functions, called a prototype something must be declared before it is used has return type, param list, no definition
basic pointer syntax
int *y = &x int* pointer = &x; //declare and initialize pointer to address of another variable
multiple pointers to the same address
int myValue = 50; int* ptrA, ptrB, ptrC; ptrA=&myValue; ptrB=&myValue; ptrC=ptrB; //same thing *ptrA=70; //changes everything
struct
like a class, but everything defaults to public accessibility
guards
#ifndef //if not defined #define //define it #endif //end definition including header file in multiple places can cause issues, particularly if you define something in a header shortcut: #pragma once
preprocessor directive
#include functions as copy/paste o Preprocessors are the directives, which give instructions to the compiler to preprocess the information before actual compilation starts. o Only whitespace chars may appears before preprocessor directive on a line o Are lines included in a program that begin with # o Uses source code (.c) and makes translation units o Go through ALL project files, one at a time, from top to bottom; perform some action as determined by directive o Errors would be syntactical or typographical
MyClass.h
#pragma once class MyClass { int foo; int severalFoos[3]{}; float otherValue; public: MyClass(int, float); int GetFoo(); };
symbol for memory address
&
linker will build
.exe from .obj (translation units)
C++ overview
C with classes created by Bjarne Stroustrup in late 70s/early 80s similar to C used in performance-critical environments- games, simulations, 3D modeling software, operating systems closer to the metal
multiple pointers to one thing
House theHouse House* partyAddress = &theHouse; House* partyAddress2 = partyAddress //copy value in partyAddress
#include <iostream>
Write the include directive needed to allow use of the various I/O operators such as cout and cin.
how to dereference
access what the pointer is pointing to float *ptr = &someValue; *ptr *= 2; //after declaration, the * dereferences the ptr accessing the value stored at that address float foo = *ptr 1- take the original type declaration- float* 2- when you dereference, take one asterisk off the original type 3- whatever is left is what you have to work with at that spot in your code
accessor/mutator
accessor- get functions, retrieve something from class mutator- set functions, change something about class behaviors- functions which do something with class data
preprocessor stage
all # directives go through all project files, one at a time, from top to bottom perform some action as determined by directive errors would be syntax/typos create translation units from results
constructors
build object in a variety of ways can have many constructors destructors destroy the object, only one per class used to initialize an object called only when an object is initially created no return type, same function name as class Foo(); Foo(int x);
data types by size
char, unsigned char, short, unsigned char, int, unsigned int, long long int, unsigned long long int, float, double
compiling stage
check all code files for proper syntax create object files from translation units compiler errors could impact other translation units, generating cascading errors
linking stage
combines various object files into library or executable file errors mean some definition is missing
no declaration
compiler error
header files
contain declarations of functions and classes .h
changing value of a pointee
dereference pointer and then assign new value int x=5, y=12; int* ptr1=&x; int* ptr2=&x; *ptr1=20; //change x to 20 cout << *ptr2; //print value of ptr2's pointee (20) x=1024;
memory addresses are in
hexadecimal
pointers have
indirect access
always
initialize variables
no definition
linker error
all variables reside somewhere in memory, called
memory address
don't leave pointers to default values, initialize to
nullptr or NULL (just 0)
default constructor
only one implicitly declared constructor, does nothing at all has no params or has params with all default arguments (a value that, if not provided, will be assigned to the param)
using namespace std
place where identifiers used in the system provided header files are defined
why pointers
pointers are a way of giving access to values (passing values to functions) without creating copies of everything
dereferencing - get value of a pointer
pointers point to mem addresses to get the data AT that mem address, we must dereference a point we must use the * again on an already-existing pointer int x=10; int* ptr=&x; cout << &x << endl; //print address of x cout << ptr << endl; //ptr is pointing to x cout << x << endl; //prints 10 cout << *ptr << endl; //prints 10 via dereferencing
3 step sequence to C++ app
preprocessing- creates translation units compiling- create object files from translation units linking- link object files into executable
public, private, protected
public- everyone can access this, use sparingly private- default section, only class itself protected- class itself and any derived classes can access this (ie inheritance)
purpose of multiple files
reduce compile time break things into logical units
preprocessor
runs before compilation, looks for #
global scope
scope outside all of the functions live outside the duration of the program shouldn't be used often
memory addresses can be used to
share access to existing variables, requiring use of pointers
differences from Java
short- 2 byte variable unsigned type- pos, 0 signed type- pos, neg, 0
scope resolution operator
std::cout indicate something belongs to something else- a class, namespace, enumeration mainly cin, cout, endl
translation units
text of program is kept in source files, together with all headers and source files included via preprocessing directives, less whitespace, is the translation unit
in C++, you don't have to use the new keyword because
the default constructor is called upon instantiation if not otherwise defined
dereferencing nullptr
throw an exception int* nullPointer = nullptr; cout << *nullPointer; //throws exception
why write classes
to group data together- no need to remember to do this yourself for multiple collections to hide the details of something to define boundaries and control access to data/functionality to reuse code
pointer
variable that contains a memory address and points to a pointee asterisk used to access what a pointer points to int* pointer (pointer-to-int)
definition
what function actually does after execution needed by the linker
can you #include a .cpp file
you can but shouldn't, since .cpp is for definitions