0% found this document useful (0 votes)
12 views8 pages

OOPS Assign

The document contains source code for 4 C++ programs: 1) binary to octal conversion using friend functions, 2) an Employee class with methods to calculate salary, 3) stack implementation using constructors and destructors, 4) a Triangle class with private side variables and constructor overloading to determine triangle type.

Uploaded by

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

OOPS Assign

The document contains source code for 4 C++ programs: 1) binary to octal conversion using friend functions, 2) an Employee class with methods to calculate salary, 3) stack implementation using constructors and destructors, 4) a Triangle class with private side variables and constructor overloading to determine triangle type.

Uploaded by

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

CS22203 – OBJECT ORIENTED PROGRAMMING

ASSIGNMENT.NO:1 DATE:17-03-2024

QUESTION:

1. Write a C++ program of binary to octal conversion using friend function.

SOURCE CODE:
#include <iostream>
using namespace std;
class octal;
class binary{
long int n;
public:
void getdata(){
cout<<"Enter Number (10 digits):";
cin>>n;
}
friend void bintooct(binary &b,octal &o);
};
class octal{
long int m;
public:
void display(){
cout<<"Octal Value is "<<m;
}
friend void bintooct(binary &b,octal &o);
};
int pow( int a,int b){
int t=1;
for (int i=0;i<b;i++){
t*=a;
}
return t;
}
void bintooct(binary &b,octal &o){
int t1,t2,t3=0,nz=0,flag=0;
while(b.n){
t1=b.n%1000;
int i=0,t4=0;
if(!(t1 || flag))nz+=1;

REGISTRATION NUMBER: 2127230501029 1|Page


while(t1){
flag=1;
t2=t1%10;
t1/=10;
t4+=t2*pow(2,i);
i++;
}
t3=t3*10+t4;
b.n/=1000;
}
long int t5=0;
while(t3){
t5=t5*10+t3%10;
t3/=10;
}
o.m=t5*pow(10,nz);
}
int main(){
binary b;
octal o;
b.getdata();
bintooct(b,o);
o.display();
return 0;
}

REGISTRATION NUMBER: 2127230501029 2|Page


CS22203 – OBJECT ORIENTED PROGRAMMING

ASSIGNMENT.NO:1 DATE:17-03-2024

QUESTION:

2 Create an 'Employee' class having the following functions and print the
final salary.
1 - 'getInfo()' which takes the salary, number of hours of work per day of
employee as parameters
2- 'AddSal()' which adds $10 to the salary of the employee if it is less
than $500.
3 - 'AddWork()' which adds $5 to the salary of the employee if the
number of hours of work per day is more than 6 hours.

SOURCE CODE:
#include <iostream>
using namespace std;
class employee{
int salary;
int nhwpd;
public:
void getInfo(int tsal,int tnhwpd){
salary=tsal;
nhwpd=tnhwpd;
}
void AddSal(){
if(salary<500)salary+=10;
}
void AddWork(){
if(nhwpd>6)salary+=5;
}
void display(){
cout<<"salary is "<<salary<<endl;
cout<<"no of hours of work per day is "<<nhwpd;
}
};
int main(){
employee e;
e.getInfo(450,8);
e.AddSal();
e.AddWork();

REGISTRATION NUMBER: 2127230501029 3|Page


e.display();
return 0;
}

REGISTRATION NUMBER: 2127230501029 4|Page


CS22203 – OBJECT ORIENTED PROGRAMMING

ASSIGNMENT.NO:1 DATE:17-03-2024

QUESTION:

3. Write a C++ program for Stack Implementation using Constructor and


Destructor

SOURCE CODE:
#include <iostream>
using namespace std;
class stack{
int stackarray[30];
int stackptr;
public:
stack(){
stackptr=0;
cout<<"Stack Initialised"<<endl;
}
void push(int val){
if (stackptr==30)cout<<"Overflow"<<endl;
else{
stackarray[stackptr]=val;
stackptr++;
}
}
void pop(){
if (!stackptr)cout<<"Underflow"<<endl;
else{
stackptr--;
cout<<stackarray[stackptr];
}
}
~stack(){
cout<<"Stack Destroyed";}
};
int main(){
stack s;
s.pop();
s.push(5);
return 0;}

REGISTRATION NUMBER: 2127230501029 5|Page


REGISTRATION NUMBER: 2127230501029 6|Page
CS22203 – OBJECT ORIENTED PROGRAMMING

ASSIGNMENT.NO:1 DATE:17-03-2024

QUESTION:

4. Create a class called Triangle that has private member variables for the
lengths of its three sides. Use all the types of Constructor to initialize the values
and Implement member functions to determine if the triangle is equilateral,
isosceles, or scalene.

SOURCE CODE:
#include <iostream>
using namespace std;
class triangle{
int s1,s2,s3;
public:
triangle(){
s1=0;
s2=0;
s3=0;
}
triangle(int ts1,int ts2,int ts3=0){
s1=ts1;
s2=ts2;
s3=ts3;}
triangle(triangle &t){
s1=t.s1;
s2=t.s2;
s3=t.s3;}
void checknature(){
if(s1==0 && s2==0 && s3==0)cout<<"TRIANGLE DOESN'T EXIST"<<endl;
else if(s1==s2 && s2==s3)cout<<"EQUILATERAL TRIANGLE"<<endl;
else if(s1!=s2 && s2!=s3)cout<<"SCALENE TRIANGLE"<<endl;
else cout<<"ISOSCELES TRIANGLE"<<endl;
}
};
int main(){
triangle t1;
triangle t2(1,2,3);
triangle t3=t2;
triangle t4(6,6,6);

REGISTRATION NUMBER: 2127230501029 7|Page


triangle t5(t4);
triangle t6(4,4);
t1.checknature();
t2.checknature();
t3.checknature();
t4.checknature();
t5.checknature();
t6.checknature();
return 0;}

REGISTRATION NUMBER: 2127230501029 8|Page

You might also like