public
Last active

Study Guide for cs3505 Midterm

  • Download Gist
cs3505Midterm.cc
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
/*CS3505 Practice Exam #1
* Asaeli Matelau
*/
 
//Declare a function named "compute" that takes two integer parameters and returns a double value
 
 
double compute(int, int);
 
//Declare a function named "vote_check" that takes references to two string values as parameters and returns true/false value.
//Do not assume that using namespace std: has been done
 
#include <string>
bool vote_check(std::string, std::string);
 
//define a function named "mpg" that takes two double parameters (gallons and miles) and returns a
//double value computing miles per gallons
 
double mpg(double gallons, double miles)
{
return miles/gallons;
}
 
//write a fragment of code that will read in 10 integers from the user, then
//display the sum of those ten integers
 
#include<iostream>
 
void summation()
{
int sum=0;
 
for(int i=0; i < 10; i++)
{
std::cout << "Please enter the next integer" <<std::endl;
int current = 0;
cin >> current;
sum = sum + current;
if(cin.fail())
{
std::cout << "The input was not valid" << std::endl;
}
}
 
std::cout << "The Summation is " << sum << std::endl;
}
 
 
//Define forward declaration, and describe why it is required in C++
 
/*
forward declarations are function prototypes, which is a declaration of a function
that includes the paramters, the function name, and return type, but does not implement the function
*/
 
//What is the purpose of a .h files? can .h files contain function declarations and definitions
 
/*
.h files are header files
Forward declarations are required by the compiler to verify the correctness of your implementation,
you can include definitions within the header file (it will compile), but header files are typically used to separate
the implementation from the interface
*/
 
//how does the compiler process #include statements?
 
/*
copies and pastes text in exactly as is where the include statement is
*/
 
//Show how you would use compiler directives in a .h file to prevent the .h file from being included
//twice. Assume the name of the file is file.h
 
#ifndef FILE_H
#define FILE_H
 
#endif
 
//What is the purpose of using the namespace statement?
//What does it allow you to avoid? what risks are associate with this statement?
 
/*
using namespace helps in avoiding conflicts between member variable names and allows for the separation of classes,
objects and functions under a group identifier. The risks associated with namespace are if you state using namespace within a header file you could change
the meaning of the code held when some other file includes that header file and unintentionally also begins using the beforementioned namespace.
*/
 
//what is the purpose of placing code within a namespace?
 
/*
The namespace statement allows you to group class, functions and objects under a name. It allows you to avoid conflicts between variables and functions by
having an addtional identifier
*/
 
//Consider the "compute" function question above. Show how you would declare the function within a namespace called cs3505
 
/*
*/
namespace cs3505{
double compute(int, int);
}
 
//assume that you are not within the cs3505 namespace. Write one line of code that would call the function from the previous function
 
cs3505::compute(int x, int y);
 
//Write two lines of code: the first line of code should create a double variable named 'd' and initialize it with 42.0.
//the second line of code should create a reference to an integer named 'pd' and initialize it so that it points to d
 
double d = 42.0;
double &pd = d;
 
//Write two lines of code: the first line of code should should create an integer variable named 'i' and initialize it with
//19. the second line of code should create a reference to an integer named 'ri' and initialize it so that it refers to i.
 
int i = 19;
int &ri = i;
 
//Write three small, separate code pieces (of one or more statements): create a new array of 100 characters on the heap,
//initialize the elements in the array (with a loop) to all contain 'X' and release the memory allocated for the array.
 
char *charArray = new char[100];
 
for (int i = 0; i < 100; i++)
charArray[i] = 'X';
 
delete [] charArray;
 
//Assume you have a variable n that is a pointer to a node in a linked list. Show three different ways of derefrencing n to get .next,
//and store it back into n. I have written the first one for you:
 
n = n[0].next;
//are these two correct?
n = n->next;
n = (*n).next;
 
//rewrite the follwing cast using the newer c++ cast syntax: (int) d
static_cast<int>(d)
 
//Consider the following const declaration int *const x = &i; assuming variables i and j are
//integers, exacly one of these statements will fail:
x= &j; // this will fail but why?
*x=4;
//Identify which statement will fail to compile and correctly explain why it will fail
 
/*
*x will dereference the pointer, and change i
x = &j; tries to change what x points to which causes an error because x is a *const
Note: & is the reference operator and can be read as "address of", * is the dereference operator and can be read as "value pointed by"
*/
 
//Show the syntax of a makefile rule. How does the 'make' program determine if it should execute the command underneath a rule?
// Show an example sufficient to explain your answer
 
rule: rule_file1.cc rule_file1.h rule_file3.cc //rulename: followed by dependent files (things to make)
g++ -g rule_file1.cc rule_file2.cc -o example // g++ compilation command (how to make it)
 
/*
make checks to see if files are out of date and if they are follows compilation commands to bring them up to date
*/
 
//What is the purpose of placing an & after a shell command?
 
/*
a & after a shell command allows it to operate in the background and leaves your terminal free to manipulate
*/
 
 
 
//Give the general form and one example for the command-line command to compile a .cc file into an executable
 
/*
g++ -o nameofExecutable.out program.cc
*/
 
//what is .o file?
/*
a .o file is an object file, the compiler creates a .o file for each source file
g++ -c file.cc //creates .o file for you to use later
 
*/
 
//What is gdb? suppose a program causes a segmentation fault.
//how would you use gdb to find where the seg. fault occurs? (just give a brief overview, exact command syntax is not required)
 
/*
GDB is a GNU debugger, to use GDB you have to enable it with the compiler by including the -g command ( g++ -g test.cc )
To run GDB you simply type GDB gdb in the current directory of your executable. To find where the seg. fault occurs, you need to run
gdb with the run command and after the seg fault occurs you use the backtrace command and read the frame state output. You can select
specific frame to get the precise location of where the seg fault occurs .ie frame x, where x is the frame of the inner most function call
*/
 
 
/************************ --OO programming and C++-- *************************************************/
 
//Write a complete class declaration for a fraction class. Name the class 'fraction' and include only a constructor that takes a numerator and denominator (as integers), accessor
//functions, and overloaded operator for +,-,*, and /. Include fields for holding a denominator and numerator (in the appropriate places)
//Include a void private helper function with no parameters named 'reduce'.
 
class fraction
{
public:
fraction(int , int ); //constructor
//accessors
int getNumerator() const;
int getDenominator() const;
 
//overloaded operators
const fraction & operator+ (const fraction & rhs);
const fraction & operator- (const fraction & rhs);
const fraction & operator* (const fraction & rhs);
const fraction & operator/ (const fraction & rhs);
 
private:
int num; // member variables
int denom;
void reduce();
 
};
 
//define the constructor for the fraction class
 
fraction(int numerator, int denominator)
{
this->num = numerator;
this->denom = denominator;
}
 
//Assume you have a class named 'measurement'. The measurement class does not inherit from the fraction class,
//but you would like to allow the measurement class access to the private section of the fraction class. How would you accomplish this? Write
// the line of code, and indicate where the line of code needs to be placed
 
/* you would need to include the friend statement within the declaration of the fraction class */
friend class measurement;
 
 
//Draw a UML class diagram that shows the following type relationship:
//A class named 'SkipList' inherits from a class named 'List'.
//We did not cover these in great detail, but I only expect rectangles with class names in them, and arrows (with hollow, open arrowheads) pointing from the edge of
//the subclass rectangle to the edge of the direct superclass rectangle
 
/*Done*/
 
//Draw a UML class diagram that shows the following type relationship: A class named "Magazine" inherits
//from a class named "Publication". A class named "Website" inherits from the class named "Publication". A class named "Blog" inherits from "Magazine" and from "Website". Finally, the blog class inherits
// only once from "Publication" (the dreaded diamond)
 
/*Done*/
 
 
//Write the first line of the class declaration for the "Skiplist" class and the "List"
//class from above. (Just write class headers that show the inheritance declarations - do not write a declaration body)
//Assume that the "SkipList" class would like to inherit the "List" class members without changing their visibility. (The default syntax won't work use the correct visbility modifier)
 
class Skiplist : public List{}; // at most the visibility will be public, List's visibility modifiers will be maintained
 
//Write the first lines of the class declarations for the "Publication", "Magazine", "Website", and "Blog" classes, as described above.
//(just write class headers that show the inheritance declarations - do not write a declaration body) Use visibility modifiers so that the Blog class cannot see any of the
//members inside of "Publication" class, even though it inherits them exactly once.
 
class Publication {};
class Magazine : private virtual Publication{};
class Website : private virtual Publication{};
class Blog : Magazine, Website{};
 
//Assume the "List" class has a function declared "int size()". Assume the "SkipList" class also has a function declared
//"int size()". If the following code runs, which size function will be executed?
 
SkipList *t = new Skiplist();
List *s = t;
s->size();
 
/*
I beleive List::size() will run. It will run as it is not declared virtual within list so it automatically calls list->size();
*/
 
//The above problem shows that function overriding is not the default behavior when inheriting classes. How would you change the
//declaration of the size function to enable polymorphism (overriding)? ( since there are two declarations, one in each class, make sure to specify which declaration you are changing.)
 
/*
Since SkipList inherits from List you would want to declare the functions virtual within List so that derived classes functions will override base class functions.
*/
 
//Write the function declaration for a pure virtual function named "getStructure", assuming it will be in the List class
// The function takes no parameters and returns a string .
 
virtual std::string getStructure()= 0; //pure virtual must include =0
 
//Where else would you need to declare and define the "getStructure" function? explain
 
/*
SkipList would need to declare and define the function as it inherits from List, so the function will be expected by the compiler.
*/
 
//Show an example of return type covariance by showing two function declarations, one inside of "List", and the other inside of "SkipList". Use a function name of "sublist", and have one of the functions return a pointer to a List object.
 
/*
Covariance: as the derived classes become more specific, so may the return types. Contravariance: as the derived classes become more specific, the parameters become more general.
Works on virtual functions
*/
 
class List{
virtual List * sublist();
};
 
class SkipList{
SkipList * sublist();
};
 
//When are destructors invoked? explain
 
/*
When the scope of the variable ends, You can also manually invoke destructors
*/
 
//when do you need to delete an object? explain
 
/*
Objects need to be deleted when they are alotted space on the heap. This happens by using the keyword new. Whenever we use the keyword new
we also need to delete the object from the heap.
*/
 
 
//define "separation of concerns"
 
/*
Separation of concerns is a design principle for separating a program into distinct sections, such that each section addresses a separate
concern. SoC is used to untangle solutions to a given task and encapsulating work done, so that it does not happen in multiple parts of a process.
.ie parse data -> output result 1 -> parse data again -> build a table -> show table results ask for input -> parse data
*/

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.