You are on page 1of 39

Object Oriented Programming Lab File

Department of Computer Science Engg.

Submitted By: Submitted To:


SHALOM GOSAIN Vivek Soi
B-Tech CSE
3rd Semester
211486
Contents

1.Basic “HELLO WORLD” Program.......................................................2


2.Basic Input Output Program.............................................................4
3.Arithmetic Operations......................................................................6
4.User Defined Function for Printing “HELL O WORLD”.....................8
5.User Defined Function for Arithmetic Operations........................10
6.Working of objects and class in C++ Programming.......................12
7.working of public and private in C++ Class....................................14
8.Demonstrate the use of static Static variables in a Function........16
9.Demonstrate static variables inside a class...................................18
10.Calculate the area of a wall..........................................................20
11.Calculate the average marks of two students.............................22
12.Overload ++ when used as prefix.................................................24
13.Overload ++ when used as prefix and postfix..............................26
14.Printing Address of Variables Using Pointer................................28
15.Printing value and Address of Variables......................................30
17.Program to display address of each element of an array...........34
................................................................................................................
.......18.Program to insert and display data entered by using pointer
notation..............................................................................................36
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
1.Basic “HELLO WORLD” Program

#include <iostream>

int main() {

std::cout << "Hello world!";

return 0;

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
2.Basic Input Output Program

#include <iostream>

using namespace std;

int main() {

int num;

cout << "Enter an integer: ";

cin >> num; // Taking input

cout << "The number is: " << num;

return 0;

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
3.Arithmetic Operations

#include <iostream>

using namespace std;

int main()

int num1, num2;

num1 = 10;

num2 = 3;

// printing the sum of num1 and num2

cout<< "num1 + num2= " << (num1 + num2) << endl;

// printing the difference of num1 and num2

cout << "num1 - num2 = " << (num1 - num2) << endl;

// printing the product of GFG1 and GFG2

cout << "num1 * num2 = " << (num1 * num2) << endl;

// printing the division of num1 by num2

cout << "num1 / num2 = " << (num1 / num2) << endl;

// printing the modulo of num1 by num2

cout << "num1 % num2 = " << (num1 % num2) << endl;

return 0;

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
4.User Defined Function for Printing “HELLO WORLD”

#include <iostream>

using namespace std;

void print();

int main()

print();

return 0;

void print()

cout << "hello World";

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
5.User Defined Function for Arithmetic Operations

#include<iostream>
using namespace std;
int add(int x,int y)
{

cout<<x+y<<endl;
return 0;

int sub(int x,int y)


{
cout<<x-y<<endl;
return 0;
}

int multi(int x,int y)


{
cout<<x*y<<endl;
return 0;
}

int Div(int x,int y)


{
cout<<x/y<<endl;
return 0;
}
int main()
{
int a,b;
cout<<"enter the value of first number=";
cin>>a;
cout<<"enter the value of second number=";
cin>>b;
add(a,b);
sub(a,b);
multi(a,b);
Div(a,b);
return 0;

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
6.Working of objects and class in C++ Programming

#include <iostream>

using namespace std;

class Employee{

public:

string Name;

string Company;

int Age;

void IntroduceYourself(){

cout << "My name is " << Name << endl;

cout << "I work in " << Company << endl;

cout << "I'm " << Age << " years old";

};

int main(){

Employee employee1;

employee1.Name = "shalom";

employee1.Company = "amazon";

employee1.Age =20;

employee1.IntroduceYourself();

return 0;

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
7.working of public and private in C++ Class

#include<iostream>
using namespace std;

class calculate
{
private:
int length;
int width;
int height;

public:
void data(int l, int w, int h)
{
length = l;
width = w;
height = h;
}
int Area()
{
return 2 * (length + width + height);
}
int Volume()
{
return length * width * height;
}
};

int main()
{
int l, w, h;
cout<<"Enter the Length, Width and Height: \n";
cin>>l>>w>>h;

calculate calc1;
calc1.data(l,w,h);

cout<<"Area = "<<calc1.Area()<<" sq units"<<endl;


cout<<"Volume = "<<calc1.Volume()<<" cubic units"<<endl;
return 0;
}

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
8.Demonstrate the use of static Static variables in a
Function

#include <iostream>
#include <string>

using namespace std;

void counter()
{

// static variable

static int count ;

cout << count << " ";

// value is updated and

// will be carried to next

// function calls

count++;
}

int main()
{

for (int i=0; i<5; i++)

counter();

return 0;
}

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
9.Demonstrate static variables inside a class

#include<iostream>

using namespace std;

class GfG
{

public:

static int i;

GfG()

{
//do nothing
};
};

int GfG :: i=0;

int main()
{

GfG obj1;

GfG obj2;

obj1.i =4;

obj2.i = 5;

// prints value of i

cout << obj1.i<<" "<<obj2.i;


}

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
10.Calculate the area of a wall

#include <iostream>

using namespace std;

class Wall {

private:

double length;

double height;

public:

// parameterized constructor to initialize variables

Wall(double len, double hgt) {

length = len;

height = hgt;

double calculateArea() {

return length * height;

};

int main() {

// create object and initialize data members

Wall wall1(15.4,17.3);

Wall wall2(22.5,27.1);

cout << "Area of Wall 1: " << wall1.calculateArea() << endl;

cout << "Area of Wall 2: " << wall2.calculateArea();

return 0;

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
}

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
11.Calculate the average marks of two students

#include <iostream>
using namespace std;

class Student {

public:
double marks;

// constructor to initialize marks


Student(double m) {
marks = m;
}
};

// function that has objects as parameters


void calculateAverage(Student s1, Student s2) {

// calculate the average of marks of s1 and s2


double average = (s1.marks + s2.marks) / 2;

cout << "Average Marks = " << average << endl;

int main() {
Student student1(95.3), student2(88.5);

// pass the objects as arguments


calculateAverage(student1, student2);

return 0;
}

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
12.Overload ++ when used as prefix

#include <iostream>
using namespace std;

class Count {
private:
int value;

public:

// Constructor to initialize count to 5


Count() : value(22) {}

// Overload ++ when used as prefix


void operator ++ () {
++value;
}

void display() {
cout << "Count: " << value << endl;
}
};

int main() {
Count count1;

// Call the "void operator ++ ()" function


++count1;

count1.display();
return 0;
}

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
13.Overload ++ when used as prefix and postfix

#include <iostream>
using namespace std;

class Count {
private:
int value;

public:

// Constructor to initialize count to 5


Count() : value(30) {}

// Overload ++ when used as prefix


void operator ++ () {
++value;
}

// Overload ++ when used as postfix


void operator ++ (int) {
value++;
}

void display() {
cout << "Count: " << value << endl;
}
};

int main() {
Count count1;

// Call the "void operator ++ (int)" function


count1++;
count1.display();

// Call the "void operator ++ ()" function


++count1;

count1.display();
return 0;
}

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
14.Printing Address of Variables Using Pointer

#include <iostream>

using namespace std;

int main()

// declare variables

int num1 = 8;

int num2 = 16;

int num3 = 32;

// print address of num1

cout << "Address of num1: "<< &num1 << endl;

// print address of num2

cout << "Address of num2: " << &num2 << endl;

// print address of num3

cout << "Address of num3: " << &num3 << endl;

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
15.Printing value and Address of Variables

#include <iostream>

using namespace std;

int main() {

int Num = 25;

// declare pointer variable

int* pointNum;

// store address of Num

pointNum = &Num;

// print value of Num

cout << "Num = " << Num << endl;

// print address of Num

cout << "Address of Num (&Num) = " << &Num << end;

// print pointer poinNum

cout << "pointNum = " << pointNum << endl;

// print the content of the address pointNum points to

cout << "Content of the address pointed to by pointNum (*pointNum) = " << *pointNum << endl;

return 0;

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
16.Changing Value Pointed by Pointers

#include <iostream>
using namespace std;
int main() {
int var = 15;
int* pointVar;

// store address of var


pointVar = &var;

// print var
cout << "var = " << var << endl;

// print *pointVar
cout << "*pointVar = " << *pointVar << endl
<< endl;

cout << "Changing value of var to 20:" << endl;

// change value of var to 20


var = 20;

// print var
cout << "var = " << var << endl;

// print *pointVar
cout << "*pointVar = " << *pointVar << endl
<< endl;

cout << "Changing value of *pointVar to 25:" << endl;

// change value of var to 25


*pointVar = 25;

// print var
cout << "var = " << var << endl;

// print *pointVar
cout << "*pointVar = " << *pointVar << endl;
return 0;
}

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
17.Program to display address of each element of an array

#include <iostream>
using namespace std;

int main()
{
float arr[3];

// declare pointer variable


float *ptr;

cout << "Displaying address using arrays: " << endl;

// use for loop to print addresses of all array elements


for (int i = 0; i < 3; ++i)
{
cout << "&arr[" << i << "] = " << &arr[i] << endl;
}

// ptr = &arr[0]
ptr = arr;

cout<<"\nDisplaying address using pointers: "<< endl;

// use for loop to print addresses of all array elements


// using pointer notation
for (int i = 0; i < 3; ++i)
{
cout << "ptr + " << i << " = "<< ptr + i << endl;
}

return 0;
}

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
OUTPUT

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE
18.Program to insert and display data entered by using
pointer notation

#include <iostream>
using namespace std;

int main() {
float arr[5];

// Insert data using pointer notation


cout << "Enter 5 numbers: ";
for (int i = 0; i < 5; ++i) {

// store input number in arr[i]


cin >> *(arr + i) ;

// Display data using pointer notation


cout << "Displaying data: " << endl;
for (int i = 0; i < 5; ++i) {

// display value of arr[i]


cout << *(arr + i) << endl ;

return 0;
}

[Type here]
Name- Shalom Gosain
Roll no- 211486
Branch - B.Tech CSE

You might also like