337 C++ (20-21) Flashcards
Will cover content from Week 6 to Week 8 Starts from basic I/O to classes (51 cards)
What do you need to include in order to use cin and cout?
#include <iostream>
What is the correct way to write cin and cout. (What is the extraction and insertion operator?)
Use cin and extraction operator, >>, to read one or more data Use cout and insertion operator << to display on the screen #include <iostream> int main() { // Example of using cin int number; std::cout << "Enter a number: "; std::cin >> number; std::cout << "You entered: " << number << std::endl; return 0; }
In order to use cin and cout in this format, what do we need to do? (Hint “using”)
Method 1:
using namespace std
Method 2:
using std::cout
using std::cin
What do the following math functions in <cmath> do? sqrt(x) pow(x, y) fabs(y) floor(x) ceil(x) exp(x) log(x)
square root
power
absolute
rounds down to the nearest int
rounds up to the nearest int
exponent
natural log
What is the basic format for a reference. Let’s say that the value we want to reference is
int ival = 1024;
int &refVal = ival;
Is this following code valid? Why or why not?
int &refval2;
int &refval2; // error, a reference must be initialized.
Let’s say that that in main we have
int main() {
int a = 45;
modify(a)
}
void modify(int & x){
x++;
}
What is x called? What is a called? (what’s their names?)
x is a reference and a is called a referent of x.
What are the two ways to type explicitly be using the type cast operator in C++?
Method 1: int x = 4; y = 7; double ratio = static_cast<double> x/y Method 2: int x = 4, y = 7; double ratio = double(x)/y;
Is the following a valid way to initialize a value?
int i(5);
Yes, it is equivalent to
int i = 5;
What are the two different ways to initialize a member
Member Initialization class_name::class_name(value_1, value_2){ member1 = value_1 member2 = value_2 } class_name::class_name(value_1, value_2): member1(value1), member2(value2){ }
What is a default constructor. What will happen if you don’t define one? In which cases will C make one for you?
Constructor that has no values passed through. Note that if you don’t define any constructors, C will make one for you. However if you define any constructor you will have to do it yourself.
What is the basic format for a getter function? Where do we place the constant?
double get_x()const;
char *get_label()const;
Since getter functions don’t need to change the values, we can add const to the end of the function declaration.
What is the correct way to use a pointer to an object?
Point c; Point *ptr; ptr = &c; cout << ptr ->get_x(); cout << (*ptr).get_x();
What is the following? Point::Point(double a, double b){ x = a; y=b;
This is a constructor
Do constructors have a return type? Can constructors be overloaded
No they don’t have a return type. Yes they can be overloaded
What is the :: operator? What does it do? Can several classes have member functions with the same name?
This is the scope resolution operator. It is used to associate a function to its corresponding class.
Yes several classes may have member functions with the same name.
What is the general format for the implementation of member functions?
return_type class_name::function_name(parameter_list){ }
Which of these is a default constructor, and which isn't? public: Point(); Point(double a, double b);
Point(); //default constructor Point(double a, double b); //non-default
What is the return type of a C++ class constructor
depends on the definition of the constructor.
True or False?
A class constructor can use the name of the class.
A class have more than one constructor.
Both statements are correct.
Let’s say that we have a getter function called get_x() in the class Point.
We then call Point a(6, 7);
How do we print the value of x?
cout << a.get_x() <<endl;
What is the difference between private and public members?
Private members can only be accessed by other members. They cannot be directly accessed using the dot operator.
Public members can be accessed from outside the class using the dot operator.
True or false, by default all class members are private.
True, all class members are private compared with struct data types where all members are public by default.
Where do we place function prototypes in classes?
We place it in public. Example:
class Point {
private:
double x;
double y;
public:
void set_x(double value);
void set_y(double value);
};