Quiz lecture 5
Quiz lecture 5
1)
Private access modifier does not allow any access to its attributes and methods which is outside
of the class, while public access modifier allows you to do so.
For example:
#include <bits/stdc++.h>
class Burger{
private:
bool beef;
bool letuce;
public:
bool tomato;
bool buns;
bool haveLetuce(){
return letuce;
}
};
int main(){
Burger BurgerKing(true,true,false,false);
cout << BurgerKing.letuce; //this is inaccessible
cout << BurgerKing.buns; //this is accessible
cout << BurgerKing.haveLetuce(); //use this method to access to
"letuce" (since it's public)
return 0;
}
2)
Class:
- A blueprint or template for creating an object
- Can define what attributes and methods an object will have
- Can be reusable
Object:
- An instance of a class
- Its attributes and methods are dependent on its class and have its own values of attributes.
- Cannot be reusable
3)
A is not true.
4)
A is wrong.
5)
a)
class Employee{
private:
int Id;
string name;
int Age;
public:
string getName(){
return name;
}
int getId(){
return Id;
}
int getAge(){
return Age;
}
};
b)
#include <bits/stdc++.h>
using namespace std;
class Employee{
private:
int Id;
string name;
int Age;
public:
Employee(int inputId){
Id = inputId;
}
string getName(){
return name;
}
int getId(){
return Id;
}
int getAge(){
return Age;
}
};
c)
class Employee{
private:
int Id;
string name;
int Age;
public:
Employee(int inputId){
Id = inputId;
}
string getName(){
return name;
}
int getId(){
return Id;
}
int getAge(){
return Age;
}
~Employee(){
cout << "This object is deleted!"
}
};
d)
#include <bits/stdc++.h>
using namespace std;
class Employee{
private:
int Id;
string name;
int Age;
public:
Employee(int inputId){
Id = inputId;
}
string getName(){
return name;
}
int getId(){
return Id;
}
int getAge(){
return Age;
}
void printInfo(){
cout << "This is the infos of this employee: \n";
cout << "Name: " << this->getName() << " \n";
cout << "Id: " << this->getId() << " \n";
cout << "Age: " << this->getAge() << " \n";
}
~Employee(){
cout << "This object is deleted!"
}
};
int main(){
Employee employee1(100,"Jack",32);
Employee employee2(101,"Mike",18);
employee1.printInfo();
employee2.printInfo();
return 0;
}