How do you initialize a new class?
Where must you write the following:
Point Point_1;
The Main() function.
What is the only type that can be used to modify the data members?
void.
What are the 2 interface types.
Public: and Private:
What does OOP stand for?
Object Oriented Programming.
How do you call a class in the main function?
Name_Of_The_Class Variable_Name;
What is usually the function of a type void function?
To print something or set something.
What all can access the data members inside of the private: section?
Constructors and Member Functions ONLY.
Public: because we want to be able to access it else where in the script?
What are examples of OOP in C++ that we already learned about. Give 2 examples.
Strings and Cin & Cout.
What is used to initialize a constructor (String, Int, Double, etc)?
You DO NOT add anything when you initialize a constructor?
List 3 differences between the getters and setters?
- Getters are any type that's not void.
- Getters can return.
- Getters cannot edit any data members in the private:.
- Setters can only be type void.
- Setters can edit values.
- Setters can print.
Spot the error: Assume all the other code segments have been added and are correct!
string Eat::munch(string item) {
eating = item;
}
There is no error! How many of you spent the whole time searching for the error?
Where all can the public: be accessed inside a script? What is another name we give to similar things?
Anywhere in the script. Global.
Name one other very famous coding language that uses OOP.
Java, Javascript, Python, Ruby, Swift, etc.
Spot the error(s)!
Class Cat {
public:
string meow();
private:
};
...
void Cat::meow() {
cout << "meow" << "\n";
return "meow";
}
- C in class is capitalized.
- function is initialized as type string but is referred to as type void.
- attempted to use "cout" inside a function of type string.
- cannot use return inside a function of type string.
You want to run a basic store and want to make an app for it. You have 18 different data members, atleast how many different member functions must you have, not including any extra ones you might have? (None of the data members are const;)
18 x 2 = 36. One getter and one setter for each data member. Technically any whole number less than 36 and greater than 1 would work, but it would not be very practical.
A discord mod wants to add a table of everyone in their server. Make a data member to address this issue. (Hint: they're thinking of banning some people).
...
private:
vector<string> members = {};
};
You and your best bud begin to code. You split up to compare results later to combine them to make something better! Here's what you both made in 5 minutes! Your friend calls you stupid! How could he, so you call him back. Are there really any errors? If so what are they?
// You:
Class part {
Public:
int set_size(int new_size);
void get_size();
Private:
int size;
};
// Buddy:
Class part {
public:
Void set_size(int new_size);
string get_size();
private:
int size;
};
You
- "C" in class is uppercase.
- "P" in public is uppercase.
- "P" in private is uppercase.
- Setter is set given type int.
- Getter is given type void.
Buddy:
- "C" is class is uppercase.
- "V" in void is uppercase.
- get_size is supposed to return type int yet is initialized as type string.
You made a wonderful program for the OOP lab and asked your best friend to check it for you. He says you made a mistake, but you tell him that he's dumb and he doesn't pay attention in class. He tells you that no, you're dumb and explains why. He says you added code inside the {} of a function inside the class. He says this will not run, but you say it will. Who's right? You or your friend?
What's the error? -- Pretend the class has already been defined. There are 3 data members, 8 member functions and one constructor.
Void Money::Money(Double change) {
total += change;
debt -= change;
}
- V in void is capitalized.
- D in double is capitalized.
- all 3 data members are not used in the constructor.
void count(int add_amnt) {
if (add_amnt >= 1000) { // Tax slabs starts at 1000.
amnt += add_amnt*(1?); // Must only add 95% of the amount. 5% tax.
tax = add_amnt*(2?);
}
else {
amnt += add_amnt;
tax = 3?;
}
}
1? 0.95
2? 0.05
3? 0.00
Under which can there be calls to const. public: or private: ?
Both! You can have member functions with const and also data members with type const.
When did C++ become an OOPL? If you can't answer you do not loose points, you can only gain.
1997.