Assignment 7
Assignment 7
Abdelhady Mostafa
4. The data members and functions of a class in C++ are by default ____________
a) public
b) private
c) None of the above
7. State whether the following statements about the constructor are True or False.
i) constructors should be declared in the private section.
ii) constructors are invoked automatically when the objects are created.
A. True,True
B. True,False
C. False,True
D. False,False
9. When struct is used instead of the keyword class means, what will happen in the
program?
a) access is public by default
b) access is private by default
c) None of the above
---------------------------
Example class:
class A
{
public:
static int value;
}
---------------------------
a) A.value
b) A::value
c) A->value
d) A^value
#include <iostream>
a) 5
#include <string>
b) Garbage value
using namespace std;
c) Error
d) None of the above
class A
MET 111 Computer and Programing Dr. Abdelhady Mostafa
{
static int a;
public:
void change(int i)
{
a = i;
}
void value_of_a()
{
cout<<a;
}
};
int main()
{
A a1 ;
a1.change(5);
a1.value_of_a();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class A
{
static int a;
public:
void change(int i){
a = i;
a) 1055
}
b) 555
void value_of_a(){
c) 101010
cout<<a;
d) 51010
}
};
int A::a = 5;
int main()
{
A a1 ;
A a2 ;
MET 111 Computer and Programing Dr. Abdelhady Mostafa
A a3 ;
a1.change(10);
a1.value_of_a();
a2.value_of_a();
a3.value_of_a();
return 0;
}
#include <iostream>
using namespace std;
class LFC
{
static int x;
public:
static void Set(int xx)
{
x = xx;
A. The program will print 0.
}
void Display() B. The program will print 33.
{ C. The program will report error.
cout<< x ;
}
};
int LFC::x = 0;
int main()
{
LFC.Set(33);
LFC.Display();
return 0;
}
#include <iostream>
using namespace std;
class Example {
public:
Example() {
cout << "Constructor Called!" << endl;
}
void display() {
cout << "Display Function Called!" <<
endl;
}
};
int main() {
Example obj;
MET 111 Computer and Programing Dr. Abdelhady Mostafa
obj.display();
return 0;
}
#include <iostream>
using namespace std;
class Test {
private:
int x = 10;
public:
void show() {
cout << "Value of x: " << x << endl;
}
};
int main() {
Test obj;
obj.show();
return 0;
}
#include <iostream>
using namespace std;
class Person {
private:
string name;
public:
Person(string n) {
name = n;
}
void introduce() {
cout << "Hello, my name is " << name
<< "." << endl;
}
};
int main() {
Person p1("Alice");
p1.introduce();
return 0;
}
#include <iostream>
using namespace std;
class Counter {
private:
static int count;
public:
MET 111 Computer and Programing Dr. Abdelhady Mostafa
Counter() {
count++;
}
static void showCount() {
cout << "Count: " << count << endl;
}
};
int Counter::count = 0;
int main() {
Counter c1, c2, c3;
Counter::showCount();
return 0;
}
#include <iostream>
using namespace std;
class Calculator {
public:
int add(int a, int b = 5) {
return a + b;
}
};
int main() {
Calculator calc;
cout << calc.add(10) << endl;
cout << calc.add(10, 20) << endl;
return 0;
}
#include <iostream>
using namespace std;
class Sample {
private:
int x;
public:
Sample(int val) : x(val) {}
Sample(const Sample &obj) {
x = obj.x;
cout << "Copy Constructor Called!" <<
endl;
}
void display() {
cout << "Value: " << x << endl;
}
};
MET 111 Computer and Programing Dr. Abdelhady Mostafa
int main() {
Sample obj1(10);
Sample obj2 = obj1; // Copy constructor
will be called
obj2.display();
return 0;
}