Docsity

C++ Programming: Concepts, Syntax, and Best Practices, Exams of Computer Science

Toggle sidebar
bg1
CSS 342: DS&A ; Second Exam
bool operator==(const Object& other) const; - ANS Equivalency operator overload signature
bool operator!=(const Object& other) const; - ANS Not equivalent operator overload signature
bool operator<=(const Object& other) const; - ANS Less than or equal to operator overload signature
bool operator>=(const Object& other) const; - ANS Greater than or equal to operator overload signature
bool operator<(const Object& other) const; - ANS Less than operator overload header
bool operator>(const Object& other) const; - ANS Greater than operator overload signature
friend ostream& operator<<(ostream& stream, const Object& rhs); - ANS Out stream (output) operator
overload header
friend istream& operator>>(istream& stream, const Object& rhs); - ANS In stream (input) operator
overload header
using namespace std; - ANS "As", so it makes it so you don't have to use "std::" when doing
istream/ostream operations.
template<class T>
void Method(T parameter); - ANS How to templatize methods.
void Swap(int& a, int& b) {
int temp = a;
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download C++ Programming: Concepts, Syntax, and Best Practices and more Exams Computer Science in PDF only on Docsity!

CSS 342: DS&A ; Second Exam

bool operator==(const Object& other) const; - ANS Equivalency operator overload signature

bool operator!=(const Object& other) const; - ANS Not equivalent operator overload signature

bool operator<=(const Object& other) const; - ANS Less than or equal to operator overload signature

bool operator>=(const Object& other) const; - ANS Greater than or equal to operator overload signature

bool operator<(const Object& other) const; - ANS Less than operator overload header

bool operator>(const Object& other) const; - ANS Greater than operator overload signature

friend ostream& operator<<(ostream& stream, const Object& rhs); - ANS Out stream (output) operator overload header

friend istream& operator>>(istream& stream, const Object& rhs); - ANS In stream (input) operator overload header

using namespace std; - ANS "As", so it makes it so you don't have to use "std::" when doing istream/ostream operations.

template

void Method(T parameter); - ANS How to templatize methods.

void Swap(int& a, int& b) {

int temp = a;

a = b;

b = temp;

} - ANS Simple swap; int can be switched out for any data type.

int data_; - ANS How to format private data members.

#include - ANS To import the iostream module, this allows input and output.

cout << "Hello World!"; - ANS Printing out the terminal, "Hello World!"

string data;

cin >> data; - ANS Taking in terminal input to string data variable.

#include - ANS Importing the string module, allows for the use of the string data type.

int* number; - ANS Pointer to int number; if printed, will print out the memory address of int number.

#ifndef OBJECT_H_

#define OBJECT_H_

class Object {

...

};

#endif - ANS General structure of a object.h file, or a header file for Object.

Object::Object() {

return Object();

} - ANS General implementation for a multiply operation overload.

Object Object::operator/(const Object& rhs) const {

...

return Object();

} - ANS General implementation for a division operation overload.

Object Object::operator-(const Object& rhs) const {

...

return Object();

} - ANS General implementation for a subtraction operation overload.

Object Object::operator+(const Object& rhs) const {

...

return Object();

} - ANS General implementation for an addition operation overload.

John Von Neumann - ANS Who had innovations in Set theory, Geometry, Quantum Mechanics, Economics, Statistics

John Von Neumann - ANS Who founded Game Theory?

John Von Neumann - ANS Who made the Monte Carlo Method?

John Von Neumann - ANS Who was the Von Neumann Architecture named after?

John Von Neumann - ANS Who was involved in the Manhattan Project?

John Von Neumann - ANS Who coined MAD?

John Von Neumann - ANS Who authored the Merge Sort Algorithm?

Why C++? - ANS Supports OOP fundamentals

Why C++? - ANS Type Safety, Operator Overloading

Why C++? - ANS Memory (stack/heap), OS, threads, usage of pointers, memory allocation/deallocation, low level functionality.

Abstraction - ANS Allows the seperation of the implementation from the expression of the Class.

ADT - ANS Abstract Data Type

Data Abstraction - ANS A collection of data and a set of well-defined operations. The data can be reused without knowing the internal data representation and structure.

Class Design Checklist - ANS • Constructors

  • Setters/Getters
  • Actions/Verbs (what the object does)
  • Operator Overloads
  • Data Members

const int kCardsInDeck = 52; - ANS Global definition of value which does not change

void MyFuntion(int p) const; - ANS Modifier on member function which does not change data in object

void MyFunction(const MyParamType& mpt); - ANS const reference to save space on passing in variable

Classes - ANS a new data type formed of a collection of data and a set of operations on the data

Data Structures - ANS a construct within a programming language that stores a collection of data

How do we do encapsulation? - ANS With driver file (main.cpp), definition files (header files), implementation files (.cpp files)

Ada Lovelace - ANS What is the most well-known name for Augustus Ada Byron, Augustus Ada Lovelace, and the Countess of Lovelace?

Ada Lovelace - ANS Who was the daughter of the famous poet, Lord Byron?

Ada Lovelace - ANS Who partnered with Charles Babbage on Analytical Engine?

Ada Lovelace - ANS Who devised / imagined first computer program? Whose algorithm to be executed by the analytical machine? Compute Bernoulli numbers?

Ada Lovelace - ANS Who was the first computer programmer?

Object operator/(const Object& rhs) const; - ANS Division operator overload signature

Object operator-(const Object& rhs) const; - ANS Subtraction operator overload header

Object operator+(const Object& rhs) const; - ANS Addition operator overload header

Object operator*(const Object& rhs) const; - ANS Multiplication operator overload header

Object operator+=(const Object& rhs) const; - ANS += operator overload header

Object operator-=(const Object& rhs) const; - ANS -= operator overload header

Object operator*=(const Object& rhs) const; - ANS *= operator overload header

Object operator/=(const Object& rhs) const; - ANS /= operator overload header

Object Object::operator+(const Object& rhs) const{

return Object(data_ + rhs.data_);

} - ANS Example addition operator overload

Object& Object::operator+=(const Object& rhs) const{

data_ += rhs;

return *this;

} - ANS Example += operator overload

ostream& Object::operator<<(ostream& stream, const Object& rhs) {

#include - ANS To include the vector module for vector objects.

vector first; - ANS Declares an empty vector.

vector second = {100, 200, 300}; - ANS Defines a vector of 100, 200, 300.

vector third = second; - ANS Makes a copy of the second vector.

vector fourth(4, 100); - ANS Creates a vector of four 100s.

i.e. {100, 100, 100, 100}

unsigned int i; - ANS Makes it so int i cannot be negative.

template

void Swap(T &x, T &y) {

T temp = x;

x = y;

y = temp;

} - ANS Templatized swap function.

template

class Object {

public:

Object(T data);

private:

T data;

} - ANS Templatized Object class.

Geoffrey Hinton - ANS Who is currently unemployed?

Geoffrey Hinton - ANS Who won the Turing Award: 2018, Deep Learning, Neural networks?

Geoffrey Hinton - ANS Who resigned from Google to "freely speak out about the risks of A.I."? Speaking out about misuse of AI, technological unemployment, existential risks.

Geoffrey Hinton - ANS Who fears AI-race between Google and Microsoft?

Geoffrey Hinton - ANS Who won the NOBEL PRIZE 2024: Physics (w/John Hofield) "for foundational discoveries and inventions that enable machine learning with artificial neural networks"?

Nicola Dell - ANS Who is an Associate Professor at Cornell University?

Nicola Dell - ANS Who co-founded the Clinic to End Tech Abuse?

Nicola Dell - ANS Who is the winner 2024 MacArthur Grant Award Winner?

vector.push_back(data); - ANS Adds "data" to the end of the vector

template void insertionSort(vector& arr) {

for (int i = 1; i < arr.size(); ++i) {

Manual Management: Memory on the heap must be manually managed using delete or free to avoid memory leaks. - ANS What gets added to the heap?

template

void bubbleSort(vector& arr) {

int n = arr.size();

for (int i = 0; i < n - 1; ++i) {

for (int j = 0; j < n - i - 1; ++j) {

if (arr[j] > arr[j + 1]) {

swap(arr[j], arr[j + 1]);

}

}

}

} - ANS Templatized Bubble Sort

string.length() - ANS How do you find the length of a string?

vector.size() - ANS How do you find the length of a vector?

sizeof(array) - ANS How do you find the length of an array?

int arr[5] = {10, 20, 30, 40, 50};

int* p = arr; // p points to arr[0]

std::cout << "First element: " << *p << std::endl; // Output: 10

p++; // Move to the next element

std::cout << "Second element: " << *p << std::endl; // Output: 20

p += 2; // Move two elements forward

std::cout << "Fourth element: " << *p << std::endl; // Output: 40

p--; // Move one element backward

std::cout << "Third element: " << *p << std::endl; // Output: 30 - ANS What code below uses pointer arithmetic to traverse an array?

Dennis Ritchie - ANS Who was the inventor of Unix with Ken Thompson?

Dennis Ritchie - ANS Who was the inventor of C?

Dennis Ritchie - ANS Who won the Turing Award in 1983?

Margaret Hamilton - ANS Who developed on-board flight software for the Apollo space program?

Margaret Hamilton - ANS Who was a director of the MIT Instruments Lab?

Margaret Hamilton - ANS Who was awarded Presidential Medal of Freedom in 2016?

Margaret Hamilton - ANS Who was a First Site Reliability Engineer (SRE)?

Margaret Hamilton - ANS Who made contributions to async software, priority scheduling and priority display?

Barbara Simons - ANS Who had a focus on scheduling and compiler optimizations?

Barbara Simons - ANS Who worked in IBM?

Recursive case - ANS part of a recursive function that includes a call to itself with modified arguments, working towards reaching the base case.

Binary - ANS Base-2 number system

Hex - ANS Base-16 number system

Octal - ANS Base-8 number system

Dynamic memory - ANS refers to memory that is allocated during runtime using operators like new and delete. Pointers to dynamically allocated memory also can be deleted.

Pointers - ANS variables that store the memory address of another variable.

When are copy constructors called? - ANS Object initialized from another object of the same class, pass- by-value to a function, object returned by value from a function, calling it manually.

Copy constructor call - ANS Object(obj), where obj is of type Object.

Copy constructor call - ANS Object obj2 = obj1, where obj1 is of type Object.

Copy constructor call - ANS method(obj), where method(Object obj)

Copy constructor call - ANS return obj, where Object method()

Decimal to Binary Conversion - ANS Divide by 2, record the remainder, update the number to the quotient of the division, repeat until quotient is 0, read remainders in reverse order.

Binary to Hexadecimal Conversion - ANS Group digits in sets of four, add leading zeros if necessary, convert each set to hexadecimal equivalent.

Binary to Octal Conversion - ANS Group digits in sets of three, add leading zeros if necessary, convert each set to octal equivalent.

Decimal to Octal Conversion - ANS Repeatedly divide the decimal number by 8 and record the remainders.

Decimal to Hexadecimal Conversion - ANS Repeatedly divide the decimal number by 16 and record the remainders.

int* ptr; - ANS How do you declare an int pointer?

int value = 42;

int* ptr = &value; - ANS How do you define an int pointer?

int* ptr = new int(42); - ANS How do you define a dynamically allocated int pointer?

delete obj; - ANS How do you delete a dynamically allocated object?

int* array = new int[size]; - ANS How do you declare a dynamically allocated int array?

delete[] array; - ANS How do you delete a dynamically allocated int array?

ptr = nullptr; - ANS How do you avoid dangling pointer?

Merge Sort : [5, 3, 8, 4, 2] : Step 3 (Split) - ANS [3] and [8]

Merge Sort : [5, 3, 8, 4, 2] : Step 4 (Merge) - ANS [3, 8]

Merge Sort : [5, 3, 8, 4, 2] : Step 5 (Split) - ANS [3, 5, 8]

Merge Sort : [5, 3, 8, 4, 2] : Step 6 (Split) - ANS [4] and [2]

Merge Sort : [5, 3, 8, 4, 2] : Step 7 (Split) - ANS [2, 4]

[2, 3, 4, 5, 8] - ANS Merge Sort : [5, 3, 8, 4, 2] : Step 8 (Split)

[5, 3, 8, 4, 2] - ANS Insertion Sort : [5, 3, 8, 4, 2] : Step 0 (Initial Array)

Insertion Sort : [5, 3, 8, 4, 2] : Step 1 - ANS [3, 5, 8, 4, 2]

Insertion Sort : [5, 3, 8, 4, 2] : Step 2 - ANS [3, 5, 8, 4, 2]

Insertion Sort : [5, 3, 8, 4, 2] : Step 3 - ANS [3, 4, 5, 8, 2]

[2, 3, 4, 5, 8] - ANS Insertion Sort : [5, 3, 8, 4, 2] : Step 4

[5, 3, 8, 4, 2] - ANS Bubble Sort : [5, 3, 8, 4, 2] : Step 0 (Initial Array)

Bubble Sort : [5, 3, 8, 4, 2] : Step 1 (Pass 1) - ANS [3, 5, 4, 2, 8]

Bubble Sort : [5, 3, 8, 4, 2] : Step 2 (Pass 2) - ANS [3, 4, 2, 5, 8]

Bubble Sort : [5, 3, 8, 4, 2] : Step 3 (Pass 3) - ANS [3, 2, 4, 5, 8]

[2, 3, 4, 5, 8] - ANS Bubble Sort : [5, 3, 8, 4, 2] : Step 4 (Pass 4)

template

void removeItem(T* arr, int& n, T item) {

int newLength = 0;

for (int i = 0; i < n; ++i) {

if (*(arr + i) != item) {

*(arr + newLength) = *(arr + i);

++newLength;

}

}

n = newLength;

} - ANS Removing all iterations of an item using pointer arithmetic.

bool PrintBase(int num, int base, string &ans) {

static string DIGIT_ARRAY = "0123456789ABCDEF";

if ((num < 0) || (base <= 0) || (base > 16)) {

return false;

}