1/39
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
How does C++ extend C?
strong type checking
new data types
Classes for implementing user defined data types
object-oriented programming (also supports - procedural and functional).
a standard library (STL) for frequently used data types (string, list, stack, queue, vector, hash,…)
generic programming, i.e., parameterization of variable types via template
Is C++ a superset of C?
Not anymore.
How do Headers in C++ differ from in C?
In standard C++, library headers are not supposed to have an extension.
C++ standard neither requires nor forbids an extension for other headers.
If a user wants to define with an extension the common choices are- .h / .hh /* .hpp
List the data types their keywords and the modifiers if applicable in C++.

In C++ what are the » and « operators called and what do they do?
With input streams, the extraction operator >> is used to remove values from the stream.
With output streams, the insertion operator << is used to put values in the stream
Give syntax examples of standard input and output.
std::cin>>i;
std::cout<<"Hello World";
State the four predefined standard stream objects, what they do and what I/O hardware they typically apply to.
Unbuffered output is typically handled immediately, where as buffered output is typically stored.

What are namespaces? Give a syntax example.
Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.
Namespaces allow us to group named entities that otherwise would have global scope into narrower scopes, giving them namespace scope.
namespace namespace_name
{
members
}What can the members of a namespace be?
Members can be:
constants,
variables,
functions,
classes,
or another namespace(nested namespaces)
What is the visibility of members in a namespace? How can members of a namespace be accessed? Give a syntax example of each.
Members are not visible outside its namespace. Two ways to access a namespace member outside its namespace:
Use namespace_name::identifier syntax
Use the using keyword to access specific or all members of a namespace
// namespaces
#include <iostream>
using namespace std;
namespace foo
{
int value() { return 5; }
}
namespace bar
{
const double pi = 3.1416;
double value() { return 2*pi; }
}
int main () {
// No need for std::cout
cout << foo::value() << '\n';
cout << bar::value() << '\n';
cout << bar::pi << '\n';
return 0;
}What does a C++ program consist of?
1 or more header files
1 or more C++ source files
Give the general syntax for defining a class in C++
class class_identifier {
class_member_list
};What are the commands for compiling and running a C++ program?
Compile: g++ hello.cpp –o helloex
Run: ./helloex
What are the members access specifiers of a class?
Private - can only be accessed by member functions (and friends) and not accessible by descendant classes
Public - an be accessed outside the class and inherited by descendant classes
Protected - can only be accessed by member functions (and friends) and inherited by descendant classes
No access specifier the default is private
What are the types of constructors?
Default Constructors (Non – parameterized Constructor)
Accepts no arguments
class_name()
Parameterized constructor
Accepts arguments
class_name(parameters)
Copy constructor
Copies another existing object
class_name (const class_name & )
& - Reference operator, used to provide an alternative name for an existing variable
Provide syntax examples of the different constructors.
class StudentInfo {
int student_id;
string name;
public:
void print();
// Default Constructor
StudentInfo()
{
student_id = 0;
name="Sam"; }
StudentInfo(int, string);
};
// Parameterized Constructor
StudentInfo::StudentInfo(int i, string s){
student_id = i;
name = s;
}How can a member function be restricted from modifying member variables?
BY using the const keyword at the end of declaration: void print() const;
What are the two ways that member functions can be declared?
By specifying the function prototype - must still be implemented separately somewhere else
By specifying the function implementation
What is an inline function and how is it made?
When a function is inline, the compiler does not make a function call. The code of the function is used in place of the function call (function call is replaced by function code and appropriate argument substitutions made). Compiled code may be slightly larger, but will execute faster because function call overhead is avoided.
Including the implementation of a function within the class definition is an implicit request (to the compiler) to make a function inline.
To explicitly request to make member functions inline add the inline keyword before return type in function declaration and definition.
Why might inline function requests be denied by the compiler?
Function is recursive
Function contains switch or goto statement
Function return type is other than void, and the return statement doesn’t exist in function body
Function contains a loop (for, while, do-while)
Function contains static variables
How can members of a class be accessed?
Member access operator, .
class Time {
public:
void set(int, int, int);
void print() const;
Time();
Time(int, int, int);
private:
int hour;
int minute;
int second;
};
myTime.set(10, 30, 0);
myTime.hour = 12; // FailsHow is the static keyword used in a class?
A static member variable is a variable that is shared by all instances of a class.
Static member variables are often used to declare class constants.
A static member function is a special member function, which is used to access only static data members
Define function overloading in C++
Create two or more member functions having the same name but different parameters declared in the same scope.
What is operator overloading in C++?
Operators have different implementations (meanings) with different arguments.
The extraction operator >> and the stream insertion operator << are overloaded. They perform the I / O operation based on the type of argument.
What is good practice in where to declare and implement Classes and Member Functions?
Good programming practice is to declare the class in a header file. Extensions: .h, .hh, .hpp
Separate the implementation of the member functions (and possibly constructors) in another source file. Extensions: .cpp, .cc, .cp
What is a key difference between Java and C++ regarding static variables?
C++ allows static local variables inside functions/methods, but Java does not.
void func() {
static int x = 0;
x++;
printf("%d\n", x);
} // Calling func() repeatedly prints: 1 2 3What is the rule regarding declaring and defining static members in a class?
Static members are only declared in a class declaration. They must be explicitly defined outside the class using the scope resolution operator. The static keyword is only used with the declaration of a static member, inside the class definition, but not with the definition of that static member.
class Time {
public:
void set(int, int, int);
void print() const;
static int getCounter();
Time();
Time(int, int, int);
private:
int hour;
int minute;
int second;
static int counter;
};
// Initialize static member variable
int Time::counter = 0;
// Define static member function
int Time::getCounter()
{
return counter;
}