0% found this document useful (0 votes)
0 views

Quiz lecture 5

quiz from the lecture

Uploaded by

Bao Huynh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Quiz lecture 5

quiz from the lecture

Uploaded by

Bao Huynh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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>

using namespace std;

class Burger{

private:
bool beef;
bool letuce;

public:
bool tomato;
bool buns;

Burger(bool inputBeef, bool inputLetuce, bool inputTomato, bool


inputBuns){
this->beef = inputBeef;
this->letuce = inputLetuce;
this->tomato = inputTomato;
this->buns = inputBuns;
}

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:

void setName(string input){


name = input;
}

void setId(int input){


Id = input;
}

void setAge(int input){


Age = input;
}

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;
}

Employee(int inputId, string inputName, int inputAge){


name = inputName;
Id = inputId;
Age = inputAge;
}

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;
}

Employee(int inputId, string inputName, int inputAge){


name = inputName;
Id = inputId;
Age = inputAge;
}

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;
}

Employee(int inputId, string inputName, int inputAge){


name = inputName;
Id = inputId;
Age = inputAge;
}

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;
}

You might also like