What is a class?
a set of objects with the same behavior
What analogy is used to show how objects of a class can all have their data members set to different values?
Clock analogy
Member functions are ________separately, _______ the class definition
defined, after
What does a constructor do?
Initializes the data members of an object
What is object-oriented programming?
Programming style in which tasks are solved by collaborating objects
What is stored in the private interface?
Data members
In a class named CashRegister, does CashRegister object has a separate copy of each data member true or false?
True
The definition of a class declares its member function where?
In the public interface
How are constructors named?
Same name as the class.
How many constructors can a class have?
Multiple!
What is stored in the public interface?
Member functions
What are the two sections within the class definition? What goes inside each section?
The Data Members section contains variables that define the object's state or characteristics. The Member Functions section contains methods that define the object's behavior and how it interacts with its data.
Where are member functions implemented?
Outside the class definition, using the : : operator.
Where are constructors declared and defined?
Declared inside the class definition; defined after the class.
What is encapsulation?
Common programming practice of making data members private so they cannot be changed directly by the user.
What does const do to a member function?
The data members in the function cannot be modified.
When defining data members, what are the steps?
need to decide which data each object needs to store,
Go through all member functions and consider their requirements
Start with your accessor functions
DEFINE!!
If you are given a class called Book, how would you correctly define a member function called open_book() outside the class?
Void Car::open_book() { cout << “Book opened!”; }
Are constructors declared as void?
No, but they don't return a value.
What type of member function, often starting with the word "get," is used to return the value of a private data member without changing it?
Accessor Function
In C++, what is the difference between an Object and a Class?
A class is the blueprint, while an object is the actual entity created in memory based on that blueprint.
Where are data members defined, and give an example using this template using variables total_price and item_count specifically?
private:
int item_count;
double total_price;
};
Detect the error:
#include <iostream>
using namespace std;
class Student {
public:
void setName(string n);
void display();
};
void setName(string n) {
name = n;
}
void Student::display() {
cout << "Student Name: " << name << endl;
}
int main() {
Student s1;
s1.setName("Alice");
s1.display();
return 0;
}
Private: {string name;}; & void student:: SetName(string n) {name = n;}
What is a default constructor?
A constructor with no parameters.
How many parameters do default constructors have?
0!