C++ Programming Language
Journal Ques:
7. Create a class called "Vehicle" which contains data members registration number
and fuel type Make getdata() function to input data value. Create class "two-Wheeler"
from vehicle which contains data member’s distance and mileage Make getdata()
function to input data. Use overloading techniques for getdata() function and display
the information with fuel used.
Code
#include<iostream>
#include<string>
using namespace std;
class vehicle{
protected:
string registration_number;
string fuel_type;
public:
virtual void getdata(){
cout<<"Enter registration number:";
cin>>registration_number;
cout<<"Enter fuel type:";
cin>>fuel_type;
}
virtual void display()const{
cout<<"registration number:"<<registration_number<<endl;
cout<<"Fuel type:"<<fuel_type<<endl;
}
};
class two_wheeler:public vehicle{
private:
float distance;
float mileage;
public:
void getdata(float dist,float mil){
distance=dist;
mileage=mil;
}
void getdata()override{
vehicle::getdata();
cout<<"Enter distance traveled(in km):";
cin>>distance;
cout<<"Enter mileage(in ltr):";
cin>>mileage;
}
void display()const override{
vehicle::display();
cout<<"distance traveled:"<<distance<<"km"<<endl;
cout<<"fuel used:"<<mileage<<"ltr"<<endl;
}
};
int main(){
two_wheeler mybike;
mybike.getdata();
mybike.display();
return 0;
}
8. Write a program that consist of two classes Time12 and Time24. The first one
maintains time on 12 hour basis, whereas the other one maintains it on 24-hour basis.
Code
#include <iostream>
#include <string> // Include string header
using namespace std;
class Time12 {
private:
int hour;
int minute;
string period;
public:
void inputTime12() {
cout << "Enter time in 12-hour format:" << endl;
cout << "Hour (1-12): ";
cin >> hour;
cout << "Minute (0-59): ";
cin >> minute;
cout << "AM or PM: ";
cin >> period;
}
void convertToTime24(int& h24, int& m24) const {
h24 = hour;
if (period == "PM" && hour != 12) {
h24 += 12;
}
if (period == "AM" && hour == 12) {
h24 = 0;
}
m24 = minute;
}
void displayTime12() const {
cout << "Time (12-hour format): " << hour << ":" << minute << " " << period << endl;
}
};
class Time24 {
private:
int hour;
int minute;
public:
void inputTime24() {
cout << "Enter time in 24-hour format:" << endl;
cout << "Hour (0-23): ";
cin >> hour;
cout << "Minute (0-59): ";
cin >> minute;
}
void convertToTime12(int& h12, int& m12, string& period) const {
h12 = hour % 12;
if (h12 == 0) h12 = 12;
m12 = minute;
period = (hour < 12) ? "AM" : "PM";
}
void displayTime24() const {
cout << "Time (24-hour format): " << hour << ":" << minute << endl;
}
};
int main() {
Time12 time12;
Time24 time24;
time12.inputTime12();
time12.displayTime12();
int h24, m24;
time12.convertToTime24(h24, m24);
cout << "Converted to 24-hour format: " << h24 << ":" << m24 << endl;
time24.inputTime24();
time24.displayTime24();
int h12, m12;
string period;
time24.convertToTime12(h12, m12, period);
cout << "Converted to 12-hour format: " << h12 << ":" << m12 << " " << period << endl;
return 0;
}
9. Create two classes DM and DB which store the values of distance. DM stores distance
in meters and centimeters. DB stores distances in feet and inches. Write a program that
can read values for the class object and add one object of DM with another object of
DB. Use a friend function to carry out the addition operation and this function will
display answer in meter and centimeters.
Code
#include <iostream>
using namespace std;
class DB; // Forward declaration of class DB
class DM {
float meters;
float centimeters;
public:
void read() {
cout << "Enter distance in meters: ";
cin >> meters;
cout << "Enter distance in centimeters: ";
cin >> centimeters;
}
friend void add(DM d, DB b);
};
class DB {
float feet;
float inches;
public:
void read() {
cout << "Enter distance in feet: ";
cin >> feet;
cout << "Enter distance in inches: ";
cin >> inches;
}
friend void add(DM d, DB b);
};
void add(DM d, DB b) {
float totalInches = (b.feet * 12) + b.inches;
float totalCentimeters = totalInches * 2.54;
float totalMetersFromDB = totalCentimeters / 100;
float totalCentimetersFromDB = totalCentimeters - (totalMetersFromDB * 100);
float finalMeters = d.meters + totalMetersFromDB;
float finalCentimeters = d.centimeters + totalCentimetersFromDB;
if (finalCentimeters >= 100) {
finalMeters += finalCentimeters / 100;
finalCentimeters = static_cast<int>(finalCentimeters) % 100;
}
cout << "Total Distance: " << finalMeters << " meters and " << finalCentimeters << "
centimeters" << endl;
}
int main() {
DM distanceInMeters;
DB distanceInFeet;
distanceInMeters.read();
distanceInFeet.read();
add(distanceInMeters, distanceInFeet);
return 0;
}
10. Write a program to maintain a telephone directory use add() and Show() methods to
add new entries and display the telephone numbers of a person when the name of the
person is given.
Code
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
class TelephoneDirectory {
private:
unordered_map<string, string> directory;
public:
void add(const string& name, const string& phoneNumber) {
directory[name] = phoneNumber;
cout << "Entry added: " << name << "-" << phoneNumber << endl;
}
void show(const string& name) const {
auto it = directory.find(name);
if (it != directory.end()) {
cout << "Phone number of " << name << ": " << it->second << endl;
} else {
cout << name << " not found in directory." << endl;
}
}
};
int main() {
TelephoneDirectory directory;
directory.add("junior", "123-456-7890");
directory.add("mark", "52-635-85201");
directory.add("adam", "892-256-63201");
cout << "\nShowing phone numbers: " << endl;
directory.show("junior");
directory.show("bob");
directory.show("charlie");
directory.show("adam");
return 0;
}
11. Create a base class shape use the class two store double type value that could be used
to compare the area. A drive to specific classes called triangle and rectangle. From the
base shape and a member in get data to the base class to initialize base data member
and another function display area.
Code
#include <iostream>
#include <math.h>
using namespace std;
class Shape {
protected:
double area;
public:
Shape() : area(0) {}
virtual void getData() = 0;
virtual void displayArea() const {
cout << "Area: " << area << endl;
}
};
class Triangle : public Shape {
private:
double base;
double height;
public:
void getData() override {
cout << "Enter base of the triangle: ";
cin >> base;
cout << "Enter height of the triangle: ";
cin >> height;
area = 0.5 * base * height;
}
void displayArea() const override {
cout << "Triangle ";
Shape::displayArea();
}
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
void getData() override {
cout << "Enter length of the rectangle: ";
cin >> length;
cout << "Enter width of the rectangle: ";
cin >> width;
area = length * width;
}
void displayArea() const override {
cout << "Rectangle ";
Shape::displayArea();
}
};
int main() {
Shape* shape;
Triangle triangle;
shape = ▵
shape->getData();
shape->displayArea();
Rectangle rectangle;
shape = &rectangle;
shape->getData();
shape->displayArea();
return 0;
}
12. Write Program to implement Stack Operations like PUSH, POP, PEEP, CHANGE
and DISPLAY.
Code
#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;
class Stack {
private:
vector<int> stack;
int maxsize;
public:
Stack(int size) : maxsize(size) {}
void push(int value) {
if (stack.size() >= maxsize) {
cout << "Stack full. Cannot push " << value << endl;
return;
}
stack.push_back(value);
cout << "Pushed " << value << " onto the stack" << endl;
}
void pop() {
if (stack.empty()) {
cout << "Stack empty" << endl;
return;
}
int value = stack.back();
stack.pop_back();
cout << "Popped " << value << " from the stack" << endl;
}
void peep() const {
if (stack.empty()) {
cout << "Stack is empty" << endl;
return;
}
cout << "Top element is " << stack.back() << endl;
}
void change(int position, int newvalue) {
if (stack.empty()) {
cout << "Stack is empty" << endl;
return;
}
if (position < 0 || position >= stack.size()) {
cout << "Invalid position" << endl;
return;
}
stack[position] = newvalue;
cout << "Changed value at position " << position << " to " << newvalue << endl;
}
void display() const {
if (stack.empty()) {
cout << "Stack is empty" << endl;
return;
}
cout << "Stack contents (top to bottom): ";
for (auto it = stack.rbegin(); it != stack.rend(); ++it) {
cout << *it << " ";
}
cout << endl;
}
};
int main() {
Stack mystack(5);
mystack.push(10);
mystack.push(20);
mystack.display();
mystack.peep();
mystack.change(2, 99);
mystack.display();
mystack.pop();
mystack.display();
mystack.pop();
mystack.pop();
mystack.pop();
return 0;
}