You are on page 1of 10

OOP ASSIGNMENT#1

Submitted By:

MUHAMMAD MAAB NAWAZ (FA21-EEE-021)


Submitted to:

MAAM NAYAB GOGOSH


DATE:

2-10-2022
TASK#1

1.
2. An abstract class is a class that is designed to be specifically used as a base class. An abstract
class contains at least one pure virtual function. You declare a pure virtual function by using a
pure specifier ( = 0 ) in the declaration of a virtual member function in the class declaration
3. The:: (scope resolution) operator is used to get hidden names due to variable scopes so
that you can still use them. The scope resolution operator can be used as both unary
and binary.
4. Use of the new operator signifies a request for the memory allocation on the heap. If the
sufficient memory is available, it initializes the memory and returns its address to the pointer
variable. The new operator should only be used if the data object should remain in memory until
delete is called. The major differences between using the new operator and not using it are as
follows:

5.
6. A friend function in C++ is defined as a function that can access private, protected and public
members of a class.The friend function is declared using the friend keyword inside the body of
the class.
Note: A friend function in C++ is a function that is declared outside a class
but can access the private and protected members of the class.

7.
“CODE”
#include <iostream>
using namespace std;
void swapvalue (int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main() {
int a, b;
cout << "Enter two numbers: \n";
cin >> a >> b;
swapvalue(&a, &b);
cout << "After swapping first and second number is "<<a <<" "<<b;
return 0;
}

TASK#2

“CODE”
#include <iostream>
using namespace std;
class Box {
// Member Variables
private:
int length;
int breadth;
int height;
// Member Functions
public:
// Constructor
Box() {
cout << " Constructor\n";
cout<<"Enter length\n";
cin>>length ;
cout<<"Enter breadth\n";
cin>>breadth ;
cout<<"Enter height\n";
cin>> height;
}
void setData(int L, int B, int H) {
length = L;
breadth = B;
height = H;
}
int getBaseArea() {
return length * breadth;
}
int getVolume() {
return length * breadth* height;
}
};
int main() {
// Creating Object of class Box
Box b1;
cout << "Base Area of Box : " << b1.getBaseArea();
cout << "\nVolume of Box : " << b1.getVolume();
return 0;
}
TASK#3

“CODE”
#include<iostream>
using namespace std;
class Human{
private:
string IColor, personName;
public:
void setName(string name){
personName = name;
}

void setIColor(string Eyecolor){


IColor = Eyecolor;
}
void display(){
cout << "Enter Eye Color : " << IColor << "\n";
cout << "Enter Person Name : " << personName <<
"\n";
}
};
int main(){
class Human h;
string Color;
cout<<"Enter the color of your eye\n";
getline(cin,Color);
cout<<"Enter Your name\n";
string Name;
getline(cin,Name);
h.setIColor(Color);
h.setName(Name);
h.display();
return 0;
}
TASK#4
“CODE”

#include<iostream>
using namespace std;
class person{
private:
char name[20];
int id;
public:
void getdetails(){
cout<<"Enter your name\n";
cin>>name;
cout<<"Enter your id : \n";
cin>>id;
}
void printdetails(){
cout<<"\nYour name is "<<name<<"\n";
cout<<"Your id is : "<<id<<"\n";
}
};
int main(){
person p;
p.getdetails();
p.printdetails();
}
TASK#5

“CODE”

#include <iostream>

using namespace std;

class Object{

private:
static int numberOfObjects;

public:

Object(){

cout<<"\nNew object made";

numberOfObjects++;

static int getNumberOfObjectALive(){

return numberOfObjects;

~Object(){

cout<<"\nObject eleminated";

numberOfObjects--;

};

int Object::numberOfObjects=0;

int main()

Object *objPointer1;

Object *objPointer2;

objPointer1 = new Object();

cout<<"\nNo. of objects created : "<<Object::getNumberOfObjectALive();

objPointer2 = new Object();

cout<<"\nNo. of objects created : "<<Object::getNumberOfObjectALive();

delete objPointer1;

cout<<"\nNo. of objects created : "<<Object::getNumberOfObjectALive();

delete objPointer2;

cout<<"\nNo. of objects created : "<<Object::getNumberOfObjectALive();

return 0;

You might also like