You are on page 1of 23

GC WOMEN UNIVERSITY, SIALKOT

DEPARTMENT OF COMPUTER SCIENCE

SUBMITTED TO: MA’AM IQRA


SUBMITTED BY: ARSHIYA KHAN
COURSE TITLE: OOP
DEPARTMENT: COMPUTER SCIENCE
COURSE CODE: CMP-111
ROLL NO: 20057

Date 12 Dec 21 4th batch, spring 2021


TABLE OF CONTENT

SR NO LAB TASKS PG NO

1 QNO 1 1

2 QNO 2 2

3 QNO 3 3

4 QNO 4 5

5 QNO 5 6

6 QNO 6 7

7 QNO 7 8

8 QNO 8 12

9 QNO 9 16

10 QNO 10 17

11 QNO 11 18

12 QNO 12 21

13 QNO 13 22
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

LAB TASK 8

QNO1 Fibonacci series C++ Program with inline function.

PROGRAM

#include <iostream>
using namespace std;
inline int fibonacci(int a, int b)
{
return a + b;
}
int main() {
int n;
cout << "Enter no. of terms :";
cin >> n;
int a = 0;
int b = 1;
int c = 0;
cout << "The fibonacii series is:\n";
cout << a << "\t";
cout << b << "\t";
for (int i = 0; i < n - 2; i++) {
c = fibonacci(a, b);
cout << c << "\t";
a = b;
b = c;
}
}

1
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

OUTPUT

Figure 1

QNO 2

Determine how to get your compiler to generate assembly code. Create a file containing
a very small function and a main () that calls the function. Generate assembly code
when the function is inlined and not inlined, and demonstrate that the inlined version
does not have the function call overhead.

PROGRAM

Before Compilation
#include <iostream>
using namespace std;
inline int add(int a, int b)
{
return a + b;
int main ()
{
int c;
c = add(4, 3);
cout << c;
}
After Compilation:
#include <iostream>

2
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

using namespace std;


int main() {
int c;
c = int(4 + 3);
cout << c;
}
ANOTHER METHODE: PROGRAM WITH AREA FINDING
With inline function
#include<iostream>
using namespace std;
inline float cir(int r)
{return (3.14*r*r);}
int main()
{
float radius,Area;
cout<<"Enter radius : ";
cin>>radius;
Area=circle(radius);
//inline processing => Area = 3.14*radius*radius;
cout<<"Area is : "<<Area<<endl;}
//when the function is inlined during the compilation process the compiler put the patch of
function directly into the main where it is being called.
Without inline function
#include<iostream>
using namespace std;
float circle(int r)
{return (3.14*r*r);}
int main()
{
float radius,Area;
cout<<"Enter radius : ";
cin>>radius;
Area=circle(radius);
cout<<"Area is : "<<Area<<endl;
}// when the function is not inlined during compilation the compiler check for only the
definition of function that whether the function is defined or not and execution is performed
during runtime.

3
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

QNO 3

Write a C++ program to get the unique enumeration values.


Expected Output:
Afghanistan = 93
Albania = 355
Algeria = 213
Andorra = 376
Angola = 244

PROGRAM

#include<iostream>
using namespace std;
enum asia { Afghanistan = 93, Albania = 355, Algeria = 213, Andorra = 376, Angola = 244};
int main() {
asia a1 = Afghanistan;
asia a2 = Albania;
asia a3 = Algeria;
asia a4 = Andorra;
asia a5 = Angola;
cout << "Afghanistan is : " << a1 << endl;
cout << "Albania is : " << a2 << endl;
cout << "Algeria is : " << a3 << endl;
cout << "Andorra is : " << a4 << endl;
cout << "Angola is : " << a5 << endl;
}

4
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

OUTPUT

Figure 2

QNO 4

Write a c++ program to get all values from an enum class.


Expected output: [93, 355, 213, 376, 244, 672]

PROGRAM

#include <iostream>
using namespace std;
class enumeration {
public:
enum color {
red = 93, blue = 355, green = 213, purple = 376, brown = 244, pink = 672
};
};
int main() {
enumeration e1;
cout << "[" << e1.red << "," << e1.blue << "," << e1.green << "," << e1.purple << "," <<
e1.brown << "," << e1.pink << "]" << endl;
}

5
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

OUTPUT

Figure 3

QNO 5

Write a C++program using Static data member to record the


occurrences of the entire object.

PROGRAM

#include<iostream>
using namespace std;
class occur {
public:
static int occ;
occur() {
++occ;
}
};
int occur::occ = 0;
int main() {
occur a1, a2, a3, a4, a5;
cout << "No. of occurences:" << a1.occ;
}

6
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

OUTPUT

Figure 4

QNO 6

Write a C++ program to use Multiple Constructor in a class for


displaying complex value.

PROGRAM

#include<iostream>
using namespace std;
class Complex
{
int Real;
int imaginary;
public:
Complex()
{
cout<<"Enter real part: ";
cin>>Real;
cout<<"Enter Complex part:";
cin>>imaginary;
cout<<Real<<"+"<<imaginary<<"i"<<endl;
}
Complex(int r,int i)
{
cout<<r<<"+"<<i<<"i"<<endl;
}
};

7
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

int main()
{
Complex c1;
Complex c2(1,2);
}

OUTPUT

Figure 5

QNO 7

Write a C++ program using Operator Overloading for


overloading Unary minus operator.

PROGRAM

#include<iostream>
using namespace std;
class sub{
public:
int a;
sub()
{
}

8
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

sub(int n)
{
a=n;
}
sub operator -()
{
a=-a;
}
};
int main()
{
sub e(5);
cout<<"Before overloading:"<<e.a<<endl;
-e;
cout<<"After overloading: "<<e.a<<endl;
}

OUTPUT

Figure 6

QNO 8

Write a C++program to display roll number, marks obtained in two subjects, sports
weight, and total score of students using class.

9
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

PROGRAM

#include<iostream>
using namespace std;
class student {
public:
int mark1;
int mark2;
int rollno;
int sport_weightage;
int total;
student() {
cout << "Enter rollno : ";
cin >> rollno;
cout << "Enter the marks in one subject : ";
cin >> mark1;
cout << "Enter marks in 2nd subject : ";
cin >> mark2;
cout << "Enter the sport weightage : ";
cin >> sport_weightage;
}
student(int m1, int m2, int r, int sp) {
mark1 = m1;
mark2 = m2;
rollno = r;
sport_weightage = sp;
}
void totl()
{
total = mark1 + mark2 + sport_weightage;
}
void display () {
cout << "\n-----------------------------------------\n";
cout << "Roll no : " << rollno << endl;
cout << "Marks in 1st subject : " << mark1 << endl;
cout << "Marks in 2nd subject : " << mark2 << endl;
cout << "Sport weightage : " << sport_weightage << endl;
cout << "Total marks : " << total << endl;
}
};
int main() {

10
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

student s1;
s1.totl();
s1.display();
}

OUTPUT

Figure 7

QNO 9

Implement a class string containing the following functions:


- Overload + operator to carry out the concatenation of strings.
- Overload = operator to carry out string copy.
- Overload <= operator to carry out the comparison of strings.
- Function to display the length of a string.
- Function tolower( ) to convert upper case letters to lower case.
- Function toupper( ) to convert lower case letters to upper case.

11
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

PROGRAM

#include<iostream>
using namespace std;
class String {
string a;
public:
void get() {
cout << "Enter string : ";
cin >> a;
}
void operator + (String s1) {
String s2;
s2.a = a + s1.a;
cout << "Strings after concatenation is : " << s2.a << endl;
}
void operator = (String s3) {
s3.a = a;
cout << "String s3 after copy operation : " << s3.a << endl;
}
void operator >= (String s4) {
if (a <= s4.a) {
cout << a << " is greater than or equal to " << s4.a << endl;
}
else
cout << a << " is not greater than or equal to " << s4.a << endl;
}
void display() {
int c;
c = a.length();
cout << "length of string " << a << " is " << c << endl;
}
void lower() {
int c;
c = a.length();
for (int i = 0; i < c; ++i) {
if (a[i] < 97) {
a[i] = a[i] + 32;
}}
cout << "string in lower case is " << a << endl;}

12
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

void upper() {
int c;
c = a.length();
for (int i = 0; i < c; ++i) {
if (a[i] >= 97) {
a[i] = a[i] - 32;
}
}
cout << "string in upper case is " << a << endl;
}
};
int main() {
String st1, st2;
st1.get();
st2.get();
st1 + st2;
st1 = st2;
st1 >= st2;
st1.display();
st2.display();
st1.lower();
st2.upper();
}

OUTPUT

Figure 8

13
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

QNO 10

Write a C++ Program to swap two numbers using call by


reference.

PROGRAM

#include<iostream>
using namespace std;
void swap(int &i,int &j)
{
int temp;
temp=i;
i=j;
j=temp;
}
int main()
{
int x=5,y=6;
cout<<"Before swap: \n x="<<x<<" y="<<y<<endl;
swap(x,y);
cout<<"After swap: \n x="<<x<<" y="<<y<<endl;
}

OUTPUT

Figure 9

14
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

QNO 11

Define a class called student that has the following data members: -
int student number
- string student name
- double student average
The following member functions: - Constructor that initialize the data members with
default values.
- set and get functions for each data member

PROGRAM

#include<iostream>
using namespace std;
class student
{
public:
int st_number;
string name;
double st_avg;
student()
{
st_number=0;
name={" "};
st_avg=0;}
void get()
{
cout<<"enter the student number : ";
cin>>st_number;
cout<<"enter the studet name : ";
cin>>name;
cout<<"enter the student average : ";
cin>>st_avg;}
void set(int num,string na,double a)
{
st_number=num;
name=na;
st_avg=a;}

15
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

void print()
{
cout<<"\n------------student data---------------\n";
cout<<"student number : "<<st_number<<endl;
cout<<"studet name : "<<name<<endl;
cout<<"student average : "<<st_avg<<endl;
}};
class under_graduate
{
public:
int level;
int year_member;
student s;
under_graduate()
{
level=0;
year_member=0;
}
void get()
{
cout<<"enter the level number: ";
cin>>level;
cout<<"enter the year member : ";
cin>>year_member;
}
void set(int l,int m)
{
level=l;
year_member=m;
}
void print()
{
cout<"\n-----------under graduation level data------------\n";
cout<<"level number : "<<level<<endl;
cout<<"year member : "<<year_member<<endl;
cout<<"student number : "<<s.st_number<<endl;
cout<<"studet name : "<<s.name<<endl;
cout<<"student average : "<<s.st_avg<<endl;}};
class master
{
public:
int id;

16
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

under_graduate g;
master()
{
id=0;
}
void get()
{
cout<<"enter the id : ";
cin>>id;
}
void set(int i)
{
id=i;
}
void print()
{
cout<<"\n----------master level data--------------\n";
cout<<"id is : "<<id<<endl;
cout<<"level number : "<<g.level<<endl;
cout<<"year member : "<<g.year_member<<endl;
cout<<"student number : "<<g.s.st_number<<endl;
cout<<"studet name : "<<g.s.name<<endl;
cout<<"student average : "<<g.s.st_avg<<endl;}};
int main()
{
student s1;
s1.set(20057,"ARSHIYA",89.9);
s1.print();
master m1;
m1.set(5648);
m1.g.s.set(20057,"ARSHIYA",80);
m1.g.set(4,2004);
m1.print();
}

17
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

OUTPUT

Figure 10

QNO NO 12

Write a C++ program to demonstrate multiple inheritances.

PROGRAM

#include<iostream>
using namespace std;
class hello

18
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

{
public:
hello()
{
cout<<"Hello whats your name?\n";
}};
class hi
{
public:
hi()
{
cout<<"hi My name is arshiya\n";
}};
class bye:public hello,hi
{
public:
bye()
{
cout<<"ok nice to meet uh bye\n";
}};
int main()
{
bye y;
}

OUTPUT

Figure 11

19
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

QNO 13

Write a C++ program to demonstrate constructor call in the


derived class

PROGRAM

#include<iostream>
using namespace std;
class Base
{
public:
Base()
{
cout<<"Constructor of base class"<<endl;
}
};
class derived:public Base
{
public:
derived()
{
cout<<"Constructor of derived class"<<endl;
}
};
int main()
{
cout<<"The order of constructor call in a derived class is as
follows:"<<endl;
derived d;
}

20
GC WOMEN UNIVERSITY, SIALKOT
DEPARTMENT OF COMPUTER SCINCE

OUTPUT

Figure 12

21

You might also like