0% found this document useful (0 votes)
117 views19 pages

Practical 1: Aim: A) Write A Program To Print "Hello World". Code

This document contains the code summaries for 7 practical assignments on Object Oriented Programming concepts: 1) The first practical covers basic C++ programs - printing text, taking user input, arithmetic operations, swapping values, and pattern printing. 2) The second practical covers OOP concepts - using classes, function overloading, and calculating perimeter using overloaded functions. 3) The third practical covers passing arguments by reference, single inheritance, and multiple inheritance. 4) Operations overloading is demonstrated by overloading arithmetic operators for a binary class.

Uploaded by

Charudatt Palvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views19 pages

Practical 1: Aim: A) Write A Program To Print "Hello World". Code

This document contains the code summaries for 7 practical assignments on Object Oriented Programming concepts: 1) The first practical covers basic C++ programs - printing text, taking user input, arithmetic operations, swapping values, and pattern printing. 2) The second practical covers OOP concepts - using classes, function overloading, and calculating perimeter using overloaded functions. 3) The third practical covers passing arguments by reference, single inheritance, and multiple inheritance. 4) Operations overloading is demonstrated by overloading arithmetic operators for a binary class.

Uploaded by

Charudatt Palvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Object Oriented Programming – Practical journal

Name Sushant Ramesh Palavi Application ID 212015


Subject Object Oriented Programming Class FYMCA
Semester Sem-1 Date
Practical 1
Aim: a) Write a program to print “Hello world”.
Code:
#include<stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
Output:
Aim: b) Write a program to print “a integer”.
Code:
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
printf("You entered: %d", number);
return 0;
}
Output:
Aim: c) Write a program to “add two numbers”.
Code:
#include <stdio.h>
int main()
{
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d",&num1,&num2);
sum = num1+num2;
printf("%d + %d = %d",num1,num2,sum);
return 0;
}
Output:
Aim: d) write a program to swap two numbers.
Code:
#include<stdio.h>
int main()
{
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
temp = first;
first = second;
second = temp;
printf("\n first number: %.2lf\n", first);
printf("\n second number: %.2lf", second);
return 0;
}
Output:
Aim: e) Write a program for the “Factorial of number”.
Code:
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
Output:
Aim: f) Write a Program to “Reverse number”.

Code:

#include <stdio.h>

int main() {

int n, rev = 0, remainder;

printf("Enter an integer: ");

scanf("%d", &n);

while (n != 0) {

remainder = n % 10;

rev = rev * 10 + remainder;

n /= 10;

printf("Reversed number = %d", rev);

return 0;

Output:
Aim: g) Write a program to print a pattern.

Code:

#include<stdio.h>
int main()
{
int a, b, c, i =1;

c = 5;
for(a=1;a<=c;a++)
{
for (b=1;b<=a;b++)
printf("%3d" ,i++);
printf("\n");
}
return 0;
}
Output:
Practical 2
Aim: a) Using class.

Code:

#include<iostream>
#include<cstring>
using namespace std;
class car
{
string Brake[20];
int speed;
public:
void slowdown( string Brake)
{
cout << " Apply Brake!" << endl;
}
void slowdown( int speed)
{
cout << "Slowdown" << endl;
}
};
int main(){
car obj;
obj.slowdown(24);
obj.slowdown(23);
return 0;
}
Output:
Aim: b) Function Overload.
Code:
#include<iostream>
using namespace std;
class sum
{
public:
int add(int num1,int num2)
{
return num1+num2;
}
int add(int num1, int num2, int num3){
return num1+num2+num3;
}
};
int main(void)
{
sum obj;
cout << obj.add(10,20,30) << endl;
cout << obj.add(25,25) << endl;
return 0;
}
Output:
Aim: c) Perimeter using Function Overload.
Code:
#include<iostream>
#include<cmath>
using namespace std;
class sum{
public:
int perimeter(int length){
return 4*length;;
}
int perimeter(int length , int breadth ){
return 2*length+2*breadth;
}
int perimeter(double radius){
return 2*M_PI*radius;
}
};
int main(void){
sum obj;
cout << obj.perimeter(23) << endl;
cout << obj.perimeter(20,40) << endl;
cout << obj.perimeter(4.5) << endl;
return 0;
}
Output:
Aim: d) Static Function.
Code:
#include<iostream>
using namespace std;
class Box
{
public:
static int objectCount;
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout << "Constrcutor called:" << endl;
length = l;
breadth = b;
height = h;
objectCount ++;
}
double volume ()
{
return length*breadth*height;
}
static int getCount()
{
return objectCount;
}
private:
double length;
double breadth;
double height;
};
int Box::objectCount =0;
int main(void)
{
cout << "Initial stage count:" << Box:: getCount() << endl;
Box Box1(3.2,3.4,4.5); // box1
Box Box2(2.5,3.5,4.5); //box2
cout << " Final stage count:" << Box:: getCount() << endl;
return 0;
}
Output:
Practical 3
Aim: a) Pass value by reference.
Code:
#include<iostream>
#include<conio.h>
using namespace std;
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
cout << " After swapping numbers \n";
cout << "a=" << a << endl;
cout << " b= " << b;
}
int main()
{
int x, y;
cout << " Enter two numbers" << endl;
cin >> x >> y;
cout << "Before Swapping number:" << endl;
cout << "a =" << x << endl;
cout << " b=" << y << endl;
swap(x,y);
getch();
return 0;
}
Output:
Aim: b) Single Inheritance.
Code:
#include<iostream>
#include<conio.h>
using namespace std;
class Employee
{
public:
int cid;
char name[20];
int age;
public:
void inputdata(int e, int a)
{
cid = e;
cout << "Enter name:" << endl;
cin >> name ;
age = a;
}
void display()
{
cout << "ID:" << cid <<endl;
cout << "Name:" << name << endl;
cout << "Age:" << age << endl;
}
};
class Fulltimestaff : public Employee
{
public:
int da;
char hra;
int salary;
public:
void enterdata(int d, char h, int sal)
{
da = d;
hra = h;
salary = sal;
}
void show()
{
cout << "Daily Allowance=" << da << endl;
cout << "HRA=" << hra << endl;
cout << "Salary=" << salary << endl;
}
};
int main()
{
Fulltimestaff f1;
f1.inputdata(30,23);
f1.display();
f1.enterdata(2500,'CE', 103000);
f1.show();
getch();
}
Output:
Aim: c) Multiple Heritance.
Code:
#include<iostream>
using namespace std;
class A
{
public:
int a=5;
A(){
cout << "Constuctor for class A"<< endl;
}
};
class B{
public:
int b=10;
B(){
cout << "Constructor for class B:" << endl;
}
};
class C: public A, public B{
public:
int c =20;
C()
{
cout << "Constructor of class C:" << endl;

cout << "Class C Inherits from class A and class B: " << endl;
}
};
int main(){
C obj;
cout << "a=" << obj.a << endl;
cout << "b=" << obj.b << endl;
cout << "c=" << obj.c << endl;
return 0;
}
Output:

Aim: d) Operation Overload.


Code:
#include<iostream>
#include<conio.h>
using namespace std;
class binary
{
private:
int a;
public:
binary()
{
}
binary(int);
void show();
binary operator+(binary);
binary operator-(binary);
binary operator*(binary);
binary operator/(binary);
};

binary::binary(int x)
{
a=x;
}
void binary::show()
{
cout<<a<<"\t";
}
binary binary::operator+(binary s)
{
s.a=a+s.a;
return s;
}
binary binary::operator-(binary s)
{
s.a=a-s.a;
return s;
}
binary binary::operator*(binary s)
{
s.a=a*s.a;
return s;
}
binary binary::operator/(binary s)
{
s.a=a/s.a;
return s;
}
int main()
{
binary ob1(100),ob2(20),ob3,ob4,ob5,ob6;
cout<<"\nThe first variable is:";
ob1.show();
cout<<"\nThe second variable is:";
ob2.show();
cout<<"\n\nOperations:";
cout<<"\n\nAddition:";
ob3=ob1+ob2;
ob3.show();
cout<<"\nSubtraction:";
ob4=ob1-ob2;
ob4.show();
cout<<"\nMultiplication:";
ob5=ob1*ob2;
ob5.show();
cout<< "\nDivision:";
ob6=ob1/ob2;
ob6.show();
return 0;
}
Output:

You might also like