0% found this document useful (0 votes)
49 views30 pages

Oodp 7

This document contains code and output for 6 questions related to OOP concepts in C++. For question 1, it defines a base class for storing basic student information and derives a class to add result information. Question 2 defines classes for basic employee information and department information, with a derived class combining them. Question 3 defines classes for getting the square and cube of a number. Question 4 extends this to include loan information. Question 5 defines classes for tracking fruit counts. Question 6 defines classes for student marks in different subjects.

Uploaded by

Bhuvan Beera
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)
49 views30 pages

Oodp 7

This document contains code and output for 6 questions related to OOP concepts in C++. For question 1, it defines a base class for storing basic student information and derives a class to add result information. Question 2 defines classes for basic employee information and department information, with a derived class combining them. Question 3 defines classes for getting the square and cube of a number. Question 4 extends this to include loan information. Question 5 defines classes for tracking fruit counts. Question 6 defines classes for student marks in different subjects.

Uploaded by

Bhuvan Beera
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/ 30

OODP WEEK 7

RA2111050010010
S S R Subramanya Hemant Konduri
QUESTION 1
Code
#include <iostream>
using namespace std;

// Base class
class std_basic_info {
private:
char name[30];
int age;
char gender;

public:
void getBasicInfo(void);
void putBasicInfo(void);
};

void std_basic_info::getBasicInfo(void)
{
cout << "Name: ";
cin >> name;
cout << "Age: ";
cin >> age;
cout << "Gender: ";
cin >> gender;
}

void std_basic_info::putBasicInfo(void)
{
cout << "Name: " << name << ",Age: " << age << ",Gender: " << gender << endl;
}

// Derived class
class std_result_info : public std_basic_info {
private:
int totalM;
float perc;
char grade;

public:
void getResultInfo(void);
void putResultInfo(void);
};

void std_result_info::getResultInfo(void)
{

cout << "Total Marks: ";


cin >> totalM;

void std_result_info::putResultInfo(void)
{
cout << "Total Marks: " << totalM<< endl;
}

int main()
{

std_result_info std;
std.getBasicInfo();
std.getResultInfo();

std.putBasicInfo();
std.putResultInfo();

return 0;
}
OUTPUT

QUESTION 2
CODE
#include <iostream>
#include <stdio.h>
using namespace std;
class basicInfo {
protected:
char name[30];
int empId;
char gender;
public:
void getBasicInfo(void)
{
cout << "Enter Name: ";
cin.getline(name, 30);
cout << "Enter Emp. Id: ";
cin >> empId;
cout << "Enter Gender: ";
cin >> gender;
}
};
class deptInfo {
protected:
char deptName[30];
char assignedWork[30];
int time2complete;

public:
void getDeptInfo(void)
{
cout << "Enter Department Name: ";
cin.ignore(1);
cin.getline(deptName, 30);
cout << "Enter assigned work: ";
fflush(stdin);
cin.getline(assignedWork, 30);
cout << "Enter time in hours to complete work: ";
cin >> time2complete;
}
};

class employee : private basicInfo, private deptInfo {


public:
void getEmployeeInfo(void)
{
cout << "Enter employee's basic info: " << endl;

getBasicInfo();
cout << "Enter employee's department info: " << endl;

getDeptInfo();
}
void printEmployeeInfo(void)
{
cout << "Employee's Information is: " << endl;
cout << "Basic Information...:" << endl;
cout << "Name: " << name << endl;
cout << "Employee ID: " << empId << endl;
cout << "Gender: " << gender << endl
<< endl;

cout << "Department Information...:" << endl;


cout << "Department Name: " << deptName << endl;
cout << "Assigned Work: " << assignedWork << endl;
cout << "Time to complete work: " << time2complete << endl;
}
};

int main()
{

employee emp;

emp.getEmployeeInfo();
emp.printEmployeeInfo();
return 0;
}
OUTPUT

QUESTION 3

#include <iostream>
using namespace std;

class Number {
private:
int num;

public:
void getNumber(void)
{
cout << "Enter an integer number: ";
cin >> num;
}

int returnNumber(void)
{
return num;
}
};

class Square : public Number {


public:
int getSquare(void)
{
int num, sqr;
num = returnNumber();
sqr = num * num;
return sqr;
}
};

class Cube : public Number {


private:
public:
int getCube(void)
{
int num, cube;
num = returnNumber();
cube = num * num * num;
return cube;
}
};

int main()
{
Square objS;
Cube objC;
int sqr, cube;

objS.getNumber();
sqr = objS.getSquare();
cout << "Square of " << objS.returnNumber() << " is: " << sqr << endl;

objC.getNumber();
cube = objC.getCube();
cout << "Cube of " << objS.returnNumber() << " is: " << cube << endl;

return 0;
}
OUTPUT

QUESTION 4

#include <iostream>
#include <stdio.h>
using namespace std;

class basicInfo {
protected:
char name[30];
int empId;
char gender;

public:
void getBasicInfo(void)
{
cout << "Enter Name: ";
cin.ignore(1);
cin.getline(name, 30);
cout << "Enter Emp. Id: ";
cin >> empId;
cout << "Enter Gender: ";
cin >> gender;
}
};

class deptInfo : private basicInfo {


protected:
char deptName[30];
char assignedWork[30];
int time2complete;

public:
void getDeptInfo(void)
{
getBasicInfo();
cout << "Enter Department Name: ";
cin.ignore(1);
cin.getline(deptName, 30);
cout << "Enter assigned work: ";
fflush(stdin);
cin.getline(assignedWork, 30);
cout << "Enter time in hours to complete work: ";
cin >> time2complete;
}
void printDeptInfo(void)
{
cout << "Employee's Information is: " << endl;
cout << "Basic Information...:" << endl;
cout << "Name: " << name << endl;
cout << "Employee ID: " << empId << endl;
cout << "Gender: " << gender << endl
<< endl;

cout << "Department Information...:" << endl;


cout << "Department Name: " << deptName << endl;
cout << "Assigned Work: " << assignedWork << endl;
cout << "Time to complete work: " << time2complete << endl;
}
};

class loanInfo : private basicInfo {


protected:
char loanDetails[30];
int loanAmount;

public:
void getLoanInfo(void)
{
getBasicInfo();
cout << "Enter Loan Details: ";
cin.ignore(1);
cin.getline(loanDetails, 30);
cout << "Enter loan amount: ";
cin >> loanAmount;
}
void printLoanInfo(void)
{
cout << "Employee's Information is: " << endl;
cout << "Basic Information...:" << endl;
cout << "Name: " << name << endl;
cout << "Employee ID: " << empId << endl;
cout << "Gender: " << gender << endl
<< endl;

cout << "Loan Information...:" << endl;


cout << "Loan Details: " << loanDetails << endl;
cout << "Loan Amount : " << loanAmount << endl;
}
};

int main()
{

deptInfo objD;

objD.getDeptInfo();
objD.printDeptInfo();

cout << endl


<< endl;

loanInfo objL;
objL.getLoanInfo();
objL.printLoanInfo();

return 0;
}
OUTPUT

QUESTION 5
CODE
#include <iostream>

using namespace std;

class Fruit

{
public:

static int fruitCounter;

Fruit()

fruitCounter++;

~Fruit()

fruitCounter--;

};

class Apples: public Fruit

public:

static int applesCounter;


Apples():Fruit()

applesCounter++;

~Apples()

applesCounter--;

};

class Mangoes: public Fruit

public:

static int mangoesCounter;

Mangoes():Fruit()
{

mangoesCounter++;

~Mangoes()

mangoesCounter--;

};

int Fruit::fruitCounter = 0;

int Apples::applesCounter = 0;

int Mangoes::mangoesCounter = 0;

int main()

Apples firstApple,secondApple,thirdApple;
Mangoes firstMango,secondMango,thirdMango,fourthMango;

cout << "Total number of fruits: " << Fruit::fruitCounter << endl;

cout << "Number of apples: " << Apples::applesCounter << endl;

cout << "Number of mangoes: " << Mangoes::mangoesCounter << endl;

return 0;

}
OUTPUT

QUESTION 6
#include <iostream>
#include <cmath>

using namespace std;

class Marks {
public:
int rollNumber;
char *name;
int *marks; // array of marks

int num_marks; // number of the marks

void GenerateMarks(int num) { // fills the array with marks


num_marks = num;
marks = new int[num];

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


marks[i] = rand() % 5 + 1;
}

int sum_of_marks() {
int sum = 0;

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


sum += marks[i];

return sum;
}

double avarage() {
return round(((double)sum_of_marks() / num_marks) * 100) / 100;
}

void set_marks(int number_of_marks, int newmarks) { // change mark with number


number_of_marks
marks[number_of_marks] = newmarks;
}
int get_marks(int number_of_marks) {
return marks[number_of_marks];
}

void set_num_marks(int num) {


num_marks = num;

delete[]marks;

marks = new int[num];


}

~Marks() {
delete[]marks;
}

};

class Physics : public Marks {


public:

};

class Chemistry : public Marks {


public:

};
class Mathematics : public Marks {
public:

};

int main()
{
int num;

cout << "Enter the number of students ";


cin >> num;

Physics *cl1_phys = new Physics[num]; // array of student with marks in physics


Chemistry *cl1_chem = new Chemistry[num]; // array of student with marks in
chemistry
Mathematics *cl1_math = new Mathematics[num]; // array of student with marks in
mathematics

for (int i = 0; i < num; i++) { // Generate marks and roll numbers

cl1_phys[i].rollNumber = cl1_chem[i].rollNumber = cl1_math[i].rollNumber


= i + 1;

cl1_phys[i].GenerateMarks(9);
cl1_chem[i].GenerateMarks(5);
cl1_math[i].GenerateMarks(2);
}

cout << "\nThe total marks of each student of a class in Physics, Chemistry and
Mathematics: \n\n";
for (int i = 0; i < num; i++) { // sums of marks
cout << "Roll number of student is " << cl1_phys[i].rollNumber << endl;

cout << "The total marks in Physics is " << cl1_phys[i].sum_of_marks() <<
endl;
cout << "The total marks in Chemistry is " << cl1_chem[i].sum_of_marks()
<< endl;
cout << "The total marks in Mathematics is " << cl1_math[i].sum_of_marks()
<< endl;

cout << endl;


}

cout << endl << "--------------------------" << endl;

cout << "\nThe average marks : \n\n";

for (int i = 0; i < num; i++) { // averages of marks


cout << "Roll number of student is " << cl1_phys[i].rollNumber << endl;

cout << "The average mark in Physics is " << cl1_phys[i].avarage() << endl;
cout << "The average mark in Chemistry is " << cl1_chem[i].avarage() <<
endl;
cout << "The average mark in Mathematics is " << cl1_math[i].avarage() <<
endl;

cout << endl;


}

return 0;
}
OUTPUT
QUESTION 7
CODE

#include<iostream>
using namespace std;
class Circle
{
protected:
float radius ;
public:
void Enter_r(void)
{
cout << "\n\t Enter the radius: "; cin >> radius ;
}
void Display_ca(void)
{
cout << "\t The area = " << (22/7 * radius*radius) ;
}
};

class Rectangle
{
protected:
float length, breadth ;
public:
void Enter_lb(void)
{
cout << "\t Enter the length : "; cin >> length ;
cout << "\t Enter the breadth : "; cin >> breadth ;
}
void Display_ar(void)
{
cout << "\t The area = " << (length * breadth);
}
};

class Cylinder : public Circle, public Rectangle


{
public:
void volume_cy(void)
{
cout << "\t The volume of the cylinder is: "
<< (22/7* radius*radius*length) ;
}
};

int main()
{
Circle c ;
cout << "\n Getting the radius of the circle\n" ;
c.Enter_r( );
c.Display_ca( );
Rectangle r ;
cout << "\n\n Getting the length and breadth of the rectangle\n\n";
r.Enter_lb( );
r.Display_ar( );
Cylinder cy ;

cout << "\n\n Getting the height and radius of the cylinder\n";
cy.Enter_r( );
cy.Enter_lb( );
cy.volume_cy( );
return 0;
}
OUTPUT

QUESTION 8
CODE
#include <iostream>
using namespace std;

class Base {
private:
int pvt = 1;

protected:
int prot = 2;

public:
int pub = 3;

// function to access private member


int getPVT() {
return pvt;
}
};

class PublicDerived : public Base {


public:
// function to access protected member from Base
int getProt() {
return prot;
}
};

int main() {
PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
return 0;
}
OUTPUT

QUESTION 9
CODE
#include <iostream>

using namespace std;

class A{
private:
int a;
protected:
int p;
public:
void get_a(int a){
this->a=a;
}
void put_a(){
cout<<"a="<<a<<endl;
}
};
class B: public A{
private:
int b;
public:
void get_b(int b){
this->b=b;
}
void get_p(int p){
this->p=p;
}
void put_b(){
cout<<"b="<<b<<endl;
}
void put_p(){
cout<<"p="<<p<<endl;
}
};
int main(){

B objB;

objB.get_a(10);
objB.get_b(20);
objB.get_p(30);

objB.put_a();
objB.put_b();
objB.put_p();

return 0;
}
OUTPUT

QUESTION 10
CODE
#include<iostream>
#include<stdio.h>

using namespace std;


class student
{
int roll;
char name[25];
char add [25];
char city[25];
public:
student()
{
cout<<" welcome in the student information system "<<endl;
}
void getdata()
{
cout<<"\n enter the student roll no. ";
cin>>roll;
cout<<"\n enter the student name ";
cin>>name;
cout<<"\n enter ther student address ";
cin>>add;
cout<<"\n enter the student city ";
cin>>city;
}
void putdata()
{
cout<<"\n the student roll no: "<<roll;
cout<<"\n the student name: "<<name;
cout<<"\n the student coty: "<<city;
}
};
class marks: public student
{
int sub1;
int sub2;
int sub3;
int per;
public:

void input()
{
getdata();
cout<<"\n enter the marks1: ";
cin>>sub1;
cout<<"\n enter the marks2: ";
cin>>sub2;
cout<<"\n enter the marks3: ";
cin>>sub3;
}
void output()
{
putdata();
cout<<"\n marks1: "<<sub1;
cout<<"\n marks2: "<<sub2;
cout<<"\n marks3: "<<sub3<<"\n";
}
void calculate ()
{
per= (sub1+sub2+sub3)/3;
cout<<"\n total percentage :: "<<per<<"\n";
}
};
int main()
{
marks m1;
int ch;
int count=0;
do
{
cout<<"\n1.input data";
cout<<"\n2.output data";
cout<<"\n3.Calculate percentage";
cout<<"\n4.exit\n";
cout<<"\nEnter the choice :: ";
cin>>ch;
switch (ch)
{
case 1:
m1.input();
count++;
break;

case 2:
m1.output();
break;

case 3:
m1.calculate();
break;
}
} while (ch!=4);
}
OUTPUT

You might also like