Вы находитесь на странице: 1из 10

COMSTAS UNIVERSITY ISLAMABAD

Islamabad Campus 

SYED SHAMS HAIDER


FA19-BEE-203\ISB
OBJECT ORIENTED PROGRAMING

SESSIONAL 1

MISS SHAMA NOREEN


ELECTRICAL ENGINEERING
Q#1: Write a C++ program with the following specifications
 
1. It will prompt the user for input of integer values. 
2. It will pass the values by reference (NOT BY ADDRESS) to
a member function defined in the class.  
3. It will change the pass by the reference values in the
member function of the class. 
4. It will use constant and non-constant objects and member
functions 
5. It will use operator overloading for the binary operator + 

CODE
#include <iostream>

using namespace std;//SYED SHAMS HAIDER FA19-BEE-203

class Add{

public:

/* Two variables that we are going to

* add. If you want to add float or double

* variables instead, just change the data

* type. for example: float num1, num2;

*/

int num1, num2;

/* This function ask the user for two numbers.

* The numbers that user enter are stored into

* num1 and num2 variables so that we can add


* them later.

*/

void ask(){

cout<<"Enter first number: ";

cin>>num1;

cout<<"Enter second number: ";

cin>>num2;

/* This function adds the numbers that are passed

* to it through arguments. I have used parameter names

* as n1 and n2 but you can choose any parameter name.

* This function returns the result.

*/

int sum(int n1, int n2){

return n1+n2;

//This function displays the addition result

void show(){

cout<<sum(num1, num2);

};

int main(){
//Creating object of class Add

Add obj;

//asking for input

obj.ask();

//Displaying the output

obj.show();

return 0;
}

DEV C++

OUTPUT
Write a C++ program to explain the following concepts 

1. Data hiding  
2. Information hiding  
3. Proxy class  

CODE
#include <iostream>

using namespace std;

struct aproxy {

aproxy(int& r) : mPtr(&r) {}
void operator = (int n) {

if (n > 1 || n < 0) {

throw "not binary digit";

*mPtr = n;

int * mPtr;

};

struct array {

int mArray[10];

aproxy operator[](int i) {

return aproxy(mArray[i]);

};

int main() {

try {

array a;

a[0] = 1; // ok

a[0] = 42; // throws exception


}

catch (const char * e) {

cout << e << endl;

}
}

Encapsulation is an Object Oriented Programming concept that binds


together the data and functions that manipulate the data, and that keeps
both safe from outside interference and misuse. Data encapsulation led to
the important OOP concept of data hiding.

class Box {

public:

double getVolume(void) {

return length * breadth * height;

private:

double length; // Length of a box

double breadth; // Breadth of a box

double height; // Height of a box

};

By Information Hiding we mean "Showing only those details to the outside


world which are Necessary for the outside world and hiding all other details
from the outside world".
#include <iostream>

using namespace std;

class implementAbstraction

private:

int a, b;

public:

// method to set values of

// private members

void set(int x, int y)

a = x;

b = y;

void display()

cout<<"a = " <<a << endl;

cout<<"b = " << b << endl;


}

};

int main()

implementAbstraction obj;

obj.set(10, 20);

obj.display();

return 0;
}

OUTPUT

Вам также может понравиться