1 Intro C++ Flashcards
Main()
- not part of any class, is a function NOT a method because of this
- must be global
- return type int, return 0 always
include
Compiler inserts the contents of the file “something” in the place where the #include statement appears
Functions vs methods
functions are outside of a class
Forward declarations
declaring a function before you implement it, so you can use it in earlier functions/methods
Syntax for making a class?
class className {};
**note ; after class declaration
Syntax for making a constructor?
className::className() { //create variablels n stuff }
** notice NO return type
Where do you define a class? Implement aa class?
- Header file!
2. .cpp file
What should a header file contain?
- Appropriate preprocessor directives
- class declaration
- public and private methods declaration
What should a class’s .cpp file contain?
- # include it’s header file
- initialize all methods of the class
Straight up meaning of
#include #ifndef #endif #define
#include = direct copy of file specified to location of this #ifndef = "if not defined" #endif = the end of any preprocessing ifs #define = defines a macro... can make #define HOP 5
What should you #include?
.h files
#define FOO #ifdef FOO // is true! // ... #endif #ifndef FOO // is false! // ... #endif
Understand!
include loops
never ending inclusion of two files
odd.h: #ifndef ODD_H #define ODD_H #include "even.h" bool odd (int x); #endif even.h: #ifndef EVEN_H #define EVEN_H #include "odd.h" bool even (int x); #endif
odd.h: #ifndef ODD_H 1 #define ODD_H 2 #include "even.h" 3 bool odd (int x); 9 #endif 10
even.h: #ifndef EVEN_H 4 #define EVEN_H 5 #include "odd.h" 6 (already defined so nothing occurs) bool even (int x); 7 #endif 8
Whats a pointer?
a type of variable which stores a memory address of another object… instead of a number or a character
How to make a pointer?
int x;
chary;
poopy *poopyhead;
Two meanings of a *?
Definition: defines type as pointer “int *x;”
Expression: dereferences a pointer… so *x will go to the object pointed at
Two meanings of a &?
Definition: makes a reference
Expression: address of x
int x =1;
make a pointer that points to x
int *x_pointer = &x;
int n =30;
int * p;
*p =n;
Walk through this code. Where does the error occur?
integer n created.
Pointer p initalized.
Value at p tried to set to n. ERROR
Error occurs because *p dereferences p, but p doesn’t have anything to dereference… so its just confused.
int n =30;
int * p;
*p =n;
Way to fix this?
int n =30;
int * p = NULL;
*p =n;
this way *p can be followed to NULL at least
What is static memory allocation? Make an aarray with static memory allocation.
-amount of space is already known when initalized
int someArray[10];
Dynamic memory allocation uses what keyword?
new
Make an array of ints with dynamic memory allocation
int *ages = new int[];
Dynamic memory allocation necessities?
delete it t the end!
Clears up memory, allows those spaces to be used, but doesn’t delete them yet!
int * ages = new int[];
How to delete a ages?
delete [] ages;
Accessing age from person object or reference?
Use person.age
Acessing age from person pointer
Use person -> age
or
(*person).age
Make it so List and ListItr can access private data from Listnode class ListNode {
public:
ListNode(); // Constructor
private:
int value;
ListNode *next, *previous; // for doubly linked lists
};
class ListNode {
public:
ListNode(); // Constructor
private:
int value;
ListNode *next, *previous; // for doubly linked lists
friend class List; friend class ListItr; };
char* x, y;
vs
char *x,y;
- is right assoocited, meaning that *x,y will be two pointers.
- x,y will be a pointer and a char
Assume int is 4 bytes
how much storage does x hold?
x=new int[10];
40 bytes throughout the life of x.
Why is dynamic memory allocation not efficient at storing data?
allocates more space than needed between elements!
References are similar to pointers how?
they both hold memory addresses
Pointer vs reference
- A reference cannot change. The address assigned to the variable cannot change to another address.
- It must be initialized upon declaration (cannot just use NULL)
- Has implicit dereferencing! Nice feature
class example1 { public: int *a; };
int main() { example1 *c = new example1; int x = 0; (*c).a = &x; // assign address of x c->a = &x; // defrerence c, then access a }
This makes sense.
How to pass a parameter by value?
int max(int a, int b); int swap(int *x, int *y); bool compare(Rational left);
How to pass a parameter by reference?
void swap(int &x, int&y);
When to use pass by reference?
When you want the ‘formal parameter’ (or the parameter as it appears in the prototype of the functions) to CHANGE the value of the actual arguments
When to use a call by constant reference?
When you DONT want the formal parameter to change!
C++ default methods
- default constructor(no parameters, doesn’t do anything at all)
- copy constructor (constructor that creates a new object, and initialized copy of the same type of object)
- destructor (~, frees up resources)
- operator=() (copy assignment operator…. makes one object already constructed copied into the other object already constructed… an operator override
Go through column 13 on deck 1! Sample code!
yeah!
Copy constructor vs assignment operator
copy constructor = new object is created from an existing object
assignment operator = already initalized object is assigned a new value from another object