Unit3 Week1 OODP
Unit3 Week1 OODP
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];
}
}
};
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;
}
};
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;
}
};
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;
}
void display() {
cout << "Total cost: Rs. " << cost << endl;
}
};
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;
}
};
int main() {
Mammals mammalObj;
MarineAnimals marineObj;
BlueWhale blueWhaleObj;
return 0;
}
PROGRAM 6 :
#include <iostream>
#include <cmath>
using namespace std;
class Shape {
public:
virtual double getArea() const = 0;
};
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);
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;
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;
};
public:
Rectangle(double l, double w) {
length = l;
width = w;
}
double area() {
return length * width;
}
};
public:
Circle(double r) {
radius = r;
}
double area() {
return 3.14 * radius * radius;
}
};
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;
};
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;
}
};
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;
};
public:
Rectangle() {
cout << "Enter length: ";
cin >> length;
cout << "Enter width: ";
cin >> width;
}
double calculate_area() {
return length * width;
}
void display_shape() {
cout << "Rectangle" << endl;
}
};
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;
};
int main() {
SavingsAccount s;
CheckingAccount c;
s.display_balance();
c.display_balance();
return 0;
}