C/C++ Flashcards
Initialize 2D ‘double’ array.
double A[3][3] = {-1.0, 0.0, 1.0; -2.0, 0.0, 2.0; -1.0, 0.0, 1.0};
How to define a function that receives a 2D array as an argument?
void my_function(double A[][5]) { …}
Basic file I/O in C
FILE *fp; fp=fopen(“test.txt”, “w”); fprintf(fp, “Testing…\n”); fclose(fp);
Explain C++ template
Template allows a function or class to work on many different data types without being rewritten for each one.
- Class templates are commonly used to implement containers.
- A function template represents a family of functions.
C++ iterator concept
In C++, an iterator is any object that, pointing to some element in a range of elements (such as an array or a container), has the ability to iterate through the elements of that range using a set of operators (at least, the increment (++) and dereference (*) operators). The concept of an iterator is fundamental to understanding the C++ Standard Template Library (STL) because iterators provide a means for accessing data stored in container classes such a vector, map, list, etc.
C++ iterator example
using namespace std;
vector myInt;
vector::iterator it; // Add to myInt
myInt.push_back(1);
myInt.push_back(4);
myInt.push_back(8);
for(it = myInt.begin(); it!= myInt.end(); it++) {
cout << “ “ << *it; //Should output 1 4 8
}
C++ reference (vs pointer)
void swap(int &v1, int &v2) { int temp = v1; v1 = v2; v2 = temp; } \* **Once reference to an object is created, then it cannot be reseated to refer a different object.** This is often done with pointers. Thus, pointer arithmetic is not possible with references. \* **Reference cannot be null.** Therefore, containers of references are NOT allowed. \* **Reference cannot be uninitialized.** int & k; // compile error!
Abstraction and Encapsulation
- Abstraction is the creation of a well-defined interface. This separates the implementation of an object from its interface.
- Encapsulation means keeps the implementation details of your abstractions PRIVATE.
C++ class definition
class Stack{ public: int pop(); void push(int i); Stack(); // constructor ~Stack(); // destructor private: int items[STACKSIZE]; int top; };
C++ Polymorphism
Dynamic typing Virtual function
C++ ‘function template’ example
// function template #include \<iostream\> using namespace std;
template <class Type>
Type GetMax (Type a, Type b) {
Type result;
result = (a>b)? a : b;
return (result);
}
int main () { int i=5, j=6, k; long l=10, m=5, n; k=GetMax\<int\>(i,j); n=GetMax\<long\>(l,m); cout \<\< k \<\< endl; cout \<\< n \<\< endl; return 0; }
C++ ‘Class template’ example
// class templates #include \<iostream\> using namespace std;
template <class Type>
class mypair {
Type a, b;
public:
mypair (Type first, Type second) {a=first; b=second;}
Type getmax ();
};
template <class Type>
Type mypair<Type>::getmax ()
{
Type retval;
retval = a>b? a : b;
return retval;
}
int main () { ** *mypair* \<int\> myobject (100, 75);** cout \<\< myobject.getmax(); return 0; }
C++ function template declaration: Two identical ways!
include <iostream>
- *template** <class identifier> function_declaration;
- *template** <typename identifier> function_declaration;
Example)
template <typename Type>
Type max(Type a, Type b) {
return a > b ? a : b;
}
int main() { // This will call max \<int\> (by argument deduction) std::cout \<\< max(3, 7) \<\< std::endl; // This will call max\<double\> (by argument deduction) std::cout \<\< max(3.0, 7.0) \<\< std::endl; // This type is ambiguous, so explicitly instantiate max\<double\> std::cout \<\< max\<double\>(3, 7.0) \<\< std::endl; return 0; }
C++ STL Vector Class
A vector is, essentially, a resizable array; the vector class allows random access via the [] operator, but adding an element anywhere but to the end of a vector causes some overhead as all of the elements are shuffled around to fit them correctly into memory.
#include \<iostream\> #include \<vector\>
using namespace std;
int main()
{
vector <int> example; //Vector to store integers
example.push_back(3); //Add 3 onto the vector
example.push_back(10); //Add 10 to the end
example.push_back(33); //Add 33 to the end
for(int x=0; x<example.size(); x++)
{
cout<<example[x]<<” “; //Should output: 3 10 33
}
if(!example.empty()) //Checks if empty
example.clear(); //Clears vector
vector <int> another_vector; //Creates another vector to store integers
another_vector.push_back(10); //Adds to end of vector
example.push_back(10); //Same
if(example==another_vector) //To show testing equality
{
example.push_back(20);
}
for(int y=0; y<example.size(); y++)
{
cout<<example[y]<<” “; //Should output 10 20
}
return 0;
}
What are the differences between Struct and Class in C++?
In C++, classes and struct are the same except for their default behaviour with regards to inheritance and access levels of members.
C++ class
Default Inheritance = private
Default Access Level for Member Variables and Functions = private
C++ struct
Default Inheritance = public
Default Access Level for Member Variables and Functions = public