0% found this document useful (0 votes)
19 views

Unit3 Week1 OODP

The document contains 9 programming problems involving OOP concepts like inheritance, polymorphism and abstract classes. Multiple classes like Student, Person, Shape are defined with behaviors like getdata(), display() etc. These classes demonstrate relationships like inheritance, overriding and polymorphism.

Uploaded by

jayaramvardhan2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Unit3 Week1 OODP

The document contains 9 programming problems involving OOP concepts like inheritance, polymorphism and abstract classes. Multiple classes like Student, Person, Shape are defined with behaviors like getdata(), display() etc. These classes demonstrate relationships like inheritance, overriding and polymorphism.

Uploaded by

jayaramvardhan2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

UNIT – 3

WEEK – 1
NAME : Naveena Sahitya Kothapalli
REG NO : RA2311050010006
CLASS : W-2
PROGRAM 1 :
#include <iostream>
#include <string>
using namespace std;

class PersonalDetails {
protected:
string name;
int rollNumber;
public:
void getPersonalDetails() {
cout << "Enter name: ";
cin >> name;
cout << "Enter Roll Number: ";
cin >> rollNumber;
}
};

class Marks {
protected:
int marks[5];
public:
void getMarks() {
cout << "Enter marks for 5 subjects:\n";
for (int i = 0; i < 5; ++i) {
cout << "Subject " << i + 1 << ": ";
cin >> marks[i];
}
}
};

class Student : public PersonalDetails, public Marks {


public:
void displayMarksheet() {
cout << "\n\nStudent Marksheet\n";
cout << "-------------------------\n";
cout << "Name: " << name << endl;
cout << "Roll Number: " << rollNumber << endl;
cout << "Marks:\n";
for (int i = 0; i < 5; ++i) {
cout << "Subject " << i + 1 << ": " << marks[i] <<
endl;
}
}
};

int main() {
Student student;
student.getPersonalDetails();
student.getMarks();
student.displayMarksheet();
return 0;
}

PROGRAM 2 :
#include <iostream>
using namespace std;

class Base {
protected:
int baseValue;
public:
Base(int value) : baseValue(value) {
cout << "Base class constructor called with value: " <<
baseValue << endl;
}
};

class Derived : public Base {


protected:
int derivedValue;
public:
Derived(int baseVal, int derivedVal) : Base(baseVal),
derivedValue(derivedVal) {
cout << "Derived class constructor called with value: " <<
derivedValue << endl;
}
};

class FurtherDerived : public Derived {


public:
FurtherDerived(int baseVal, int derivedVal, int furtherDerivedVal)
: Derived(baseVal, derivedVal) {
cout << "FurtherDerived class constructor called with value: "
<< furtherDerivedVal << endl;
}
};

int main() {
FurtherDerived fd(10, 20, 30);
return 0;
}
PROGRAM 3 :
#include <iostream>
#include <string>
using namespace std;

class Student {
protected:
string name;
int registerNo;
public:
void getStudentDetails() {
cout << "Enter name of the student: ";
getline(cin, name);
cout << "Enter register number: ";
cin >> registerNo;
}
};

class ExamDetails : public Student {


protected:
string subjectName;
string subjectCode;
int internalMarks;
int externalMarks;
public:
void getExamDetails() {
cout << "Enter subject name: ";
cin.ignore();
getline(cin, subjectName);
cout << "Enter subject code: ";
cin >> subjectCode;
cout << "Enter internal marks: ";
cin >> internalMarks;
cout << "Enter external marks: ";
cin >> externalMarks;
}

void displayMarksheet() {
cout << "\n\nStudent Marksheet\n";
cout << "-------------------------\n";
cout << "Name: " << name << endl;
cout << "Register Number: " << registerNo << endl;
cout << "Subject Name: " << subjectName << endl;
cout << "Subject Code: " << subjectCode << endl;
cout << "Internal Marks: " << internalMarks << endl;
cout << "External Marks: " << externalMarks << endl;
}
};

int main() {
ExamDetails student;
student.getStudentDetails();
student.getExamDetails();
student.displayMarksheet();
return 0;
}
PROGRAM 4 :
#include <iostream>
using namespace std;

class Electricity {
protected:
float units;
float cost;
public:
void input() {
cout << "Enter total units consumed: ";
cin >> units;
}

virtual void bill() {


if (units <= 100)
cost = units * 0.5;
else if (units > 100 && units <= 300)
cost = 100 * 0.5 + (units - 100) * 0.6;
else
cost = 100 * 0.5 + 200 * 0.6 + (units - 300) * 0.6;
}

void display() {
cout << "Total cost: Rs. " << cost << endl;
}
};

class More_Electricity : public Electricity {


public:
void bill() override {
Electricity::bill();
if (cost > 250)
cost += (cost - 250) * 0.15;
}
};

int main() {
More_Electricity user;
user.input();
user.bill();
user.display();
return 0;
}
PROGRAM 5 :
#include <iostream>
using namespace std;

class Mammals {
public:
void print() {
cout << "I am mammal" << endl;
}
};

class MarineAnimals {
public:
void print() {
cout << "I am a marine animal" << endl;
}
};

class BlueWhale : public Mammals, public MarineAnimals {


public:
void print() {
Mammals::print();
MarineAnimals::print();
cout << "I belong to both the categories: Mammals as well
as Marine Animals" << endl;
}
};

int main() {
Mammals mammalObj;
MarineAnimals marineObj;
BlueWhale blueWhaleObj;

cout << "Calling function of Mammals by the object of


Mammals: ";
mammalObj.print();

cout << "\nCalling function of MarineAnimal by the object of


MarineAnimal: ";
marineObj.print();

cout << "\nCalling function of BlueWhale by the object of


BlueWhale: ";
blueWhaleObj.print();

cout << "\nCalling function of each of its parent by the


object of BlueWhale: " << endl;
cout << "Function of Mammals: ";
blueWhaleObj.Mammals::print();
cout << "Function of MarineAnimals: ";
blueWhaleObj.MarineAnimals::print();

return 0;
}
PROGRAM 6 :
#include <iostream>
#include <cmath>
using namespace std;

class Shape {
public:
virtual double getArea() const = 0;
};

class Circle: public Shape {


private:
double radius;
public:
Circle(double r) : radius(r) {}
double getArea() const override {
return M_PI * radius * radius;
}
};

class Rectangle: public Shape {


private:
double length, breadth;
public:
Rectangle(double l, double b) : length(l), breadth(b) {}
double getArea() const override {
return length * breadth;
}
};

class Cylinder: public Circle {


private:
double height;
public:
Cylinder(double r, double h) : Circle(r), height(h) {}
double getVolume() const {
return getArea() * height;
}
};

int main() {
double l, b, r, h;
cout << "Enter length, breadth, and radius: ";
cin >> l >> b >> r;
cout << "Enter height: ";
cin >> h;
Rectangle rect(l, b);
Circle circ(r);
Cylinder cyl(r, h);

cout << "Area of rectangle: " << rect.getArea() << endl;


cout << "Area of circle: " << circ.getArea() << endl;
cout << "Volume of cylinder: " << cyl.getVolume() << endl;

return 0;
}
PROGRAM 7 :
#include <iostream>
using namespace std;

class Person {
public:
string name;
int age;
static int id;
Person() {
id++;
}
virtual void getdata() {
cout << "Enter name and age: ";
cin >> name >> age;
}
virtual void putdata() {
cout << "Name: " << name << ", Age: " << age << ", ID: "
<< id << endl;
}
};

int Person::id = 0;

class Professor: public Person {


public:
int publications;
int cur_id;
Professor() {
cur_id = id;
}
void getdata() {
Person::getdata();
cout << "Enter publications: ";
cin >> publications;
}
void putdata() {
Person::putdata();
cout << "Publications: " << publications << ", ID: " <<
cur_id << endl;
}
};

class Student: public Person {


public:
int marks[6];
int cur_id;
Student() {
cur_id = id;
}
void getdata() {
Person::getdata();
cout << "Enter marks: ";
for (int i = 0; i < 6; i++) {
cin >> marks[i];
}
}
void putdata() {
Person::putdata();
int sum = 0;
for (int i = 0; i < 6; i++) {
sum += marks[i];
}
cout << "Sum of marks: " << sum << ", ID: " << cur_id <<
endl;
}
};

int main() {
Professor p;
p.getdata();
p.putdata();

Student s;
s.getdata();
s.putdata();

return 0;
}

PROGRAM 8 :
#include <iostream>
using namespace std;
class Shape {
public:
virtual double area() = 0;
};

class Rectangle : public Shape {


private:
double length;
double width;

public:
Rectangle(double l, double w) {
length = l;
width = w;
}
double area() {
return length * width;
}
};

class Circle : public Shape {


private:
double radius;

public:
Circle(double r) {
radius = r;
}
double area() {
return 3.14 * radius * radius;
}
};

class Square : public Shape {


private:
double side;

public:
Square(double s) {
side = s;
}
double area() {
return side * side;
}
};

int main() {
double r, l, w, s;
cout << "Enter radius of circle: ";
cin >> r;
Circle c(r);
cout << "Enter length and width of rectangle: ";
cin >> l >> w;
Rectangle rec(l, w);
cout << "Enter side of square: ";
cin >> s;
Square sq(s);
cout << "\nArea of circle: " << c.area()
<< "\nArea of rectangle: " << rec.area()
<< "\nArea of square: " << sq.area() << endl;
return 0;
}

PROGRAM 9 :
#include <iostream>
using namespace std;

class Base {
public:
virtual void display() = 0;
};

class Derived1 : public Base {


public:
void display() {
cout << "Inside Derived1" << endl;
}
};

class Derived2 : public Base {


public:
void display() {
cout << "Inside Derived2" << endl;
}
};

class Derived3 : public Derived1, public Derived2 {


public:
void show() {
Derived1::display(); // explicitly calling the display()
function of Derived1
}
};

int main() {
Derived3 d3;
d3.show();
return 0;
}

PROGRAM 10 :
#include <iostream>
using namespace std;

class Vehicle {
public:
virtual void start() = 0;
virtual void stop() = 0;
};
class Car : public Vehicle {
public:
void start() {
cout << "Car started" << endl;
}
void stop() {
cout << "Car stopped" << endl;
}
};

class Motorcycle : public Vehicle {


public:
void start() {
cout << "Motorcycle started" << endl;
}
void stop() {
cout << "Motorcycle stopped" << endl;
}
};

int main() {
Car c;
Motorcycle m;
c.start();
c.stop();
m.start();
m.stop();
return 0;
}
PROGRAM 11 :
#include <iostream>
#include <cmath>
using namespace std;

class Shape {
public:
virtual double calculate_area() = 0;
virtual void display_shape() = 0;
};

class Rectangle : public Shape {


private:
double length;
double width;

public:
Rectangle() {
cout << "Enter length: ";
cin >> length;
cout << "Enter width: ";
cin >> width;
}
double calculate_area() {
return length * width;
}
void display_shape() {
cout << "Rectangle" << endl;
}
};

class Circle : public Shape {


private:
double radius;

public:
Circle() {
cout << "Enter radius: ";
cin >> radius;
}
double calculate_area() {
return M_PI * radius * radius;
}
void display_shape() {
cout << "Circle" << endl;
}
};

int main() {
Rectangle r;
Circle c;
cout << "Area of rectangle: " << r.calculate_area() << endl;
r.display_shape();
cout << "Area of circle: " << c.calculate_area() << endl;
c.display_shape();
return 0;
}

PROGRAM 12 :
#include <iostream>
using namespace std;

class Bank {
public:
virtual void display_balance() = 0;
double balance;
};

class SavingsAccount : public Bank {


public:
SavingsAccount() {
balance = 1000;
}
void display_balance() {
cout << "Savings Account Balance: " << balance << endl;
}
};
class CheckingAccount : public Bank {
public:
CheckingAccount() {
balance = 500;
}
void display_balance() {
cout << "Checking Account Balance: " << balance << endl;
}
};

int main() {
SavingsAccount s;
CheckingAccount c;
s.display_balance();
c.display_balance();
return 0;
}

You might also like