1 Intro C++ Flashcards

1
Q

Main()

A
  • not part of any class, is a function NOT a method because of this
  • must be global
  • return type int, return 0 always
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

include

A

Compiler inserts the contents of the file “something” in the place where the #include statement appears

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Functions vs methods

A

functions are outside of a class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Forward declarations

A

declaring a function before you implement it, so you can use it in earlier functions/methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Syntax for making a class?

A

class className {};

**note ; after class declaration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Syntax for making a constructor?

A
className::className() {
//create variablels n stuff
}

** notice NO return type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Where do you define a class? Implement aa class?

A
  1. Header file!

2. .cpp file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What should a header file contain?

A
  • Appropriate preprocessor directives
  • class declaration
  • public and private methods declaration
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What should a class’s .cpp file contain?

A
  • # include it’s header file

- initialize all methods of the class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Straight up meaning of

#include
#ifndef
#endif
#define
A
#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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What should you #include?

A

.h files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
#define FOO
#ifdef FOO   // is true!
// ...
#endif
#ifndef FOO  // is false!
// ...
#endif
A

Understand!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

include loops

A

never ending inclusion of two files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
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
A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Whats a pointer?

A

a type of variable which stores a memory address of another object… instead of a number or a character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to make a pointer?

A

int x;
char
y;
poopy *poopyhead;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Two meanings of a *?

A

Definition: defines type as pointer “int *x;”

Expression: dereferences a pointer… so *x will go to the object pointed at

18
Q

Two meanings of a &?

A

Definition: makes a reference

Expression: address of x

19
Q

int x =1;

make a pointer that points to x

A

int *x_pointer = &x;

20
Q

int n =30;
int * p;
*p =n;

Walk through this code. Where does the error occur?

A

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.

21
Q

int n =30;
int * p;
*p =n;

Way to fix this?

A

int n =30;
int * p = NULL;
*p =n;

this way *p can be followed to NULL at least

22
Q

What is static memory allocation? Make an aarray with static memory allocation.

A

-amount of space is already known when initalized

int someArray[10];

23
Q

Dynamic memory allocation uses what keyword?

A

new

24
Q

Make an array of ints with dynamic memory allocation

A

int *ages = new int[];

25
Q

Dynamic memory allocation necessities?

A

delete it t the end!

Clears up memory, allows those spaces to be used, but doesn’t delete them yet!

26
Q

int * ages = new int[];

How to delete a ages?

A

delete [] ages;

27
Q

Accessing age from person object or reference?

A

Use person.age

28
Q

Acessing age from person pointer

A

Use person -> age

or

(*person).age

29
Q
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
};

A

class ListNode {

public:
ListNode(); // Constructor

private:
int value;
ListNode *next, *previous; // for doubly linked lists

    friend class List;
    friend class ListItr;
};
30
Q

char* x, y;
vs
char *x,y;

A
  • is right assoocited, meaning that *x,y will be two pointers.
  • x,y will be a pointer and a char
31
Q

Assume int is 4 bytes

how much storage does x hold?

x=new int[10];

A

40 bytes throughout the life of x.

32
Q

Why is dynamic memory allocation not efficient at storing data?

A

allocates more space than needed between elements!

33
Q

References are similar to pointers how?

A

they both hold memory addresses

34
Q

Pointer vs reference

A
  1. A reference cannot change. The address assigned to the variable cannot change to another address.
  2. It must be initialized upon declaration (cannot just use NULL)
  3. Has implicit dereferencing! Nice feature
35
Q
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
}
A

This makes sense.

36
Q

How to pass a parameter by value?

A
int max(int a, int b);
int swap(int *x, int *y);
bool compare(Rational left);
37
Q

How to pass a parameter by reference?

A

void swap(int &x, int&y);

38
Q

When to use pass by reference?

A

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

39
Q

When to use a call by constant reference?

A

When you DONT want the formal parameter to change!

40
Q

C++ default methods

A
  • 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
41
Q

Go through column 13 on deck 1! Sample code!

A

yeah!

42
Q

Copy constructor vs assignment operator

A

copy constructor = new object is created from an existing object

assignment operator = already initalized object is assigned a new value from another object