0% found this document useful (0 votes)
7 views11 pages

OODP Week 4 Programs

The document contains a series of C++ programs demonstrating object-oriented programming concepts such as encapsulation, inheritance, and class methods. Each program defines a class with various attributes and methods, showcasing functionalities like calculating area and perimeter, managing item quantities, and handling student records. The document includes code snippets and their expected outputs for each program.

Uploaded by

poke.cool1000
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)
7 views11 pages

OODP Week 4 Programs

The document contains a series of C++ programs demonstrating object-oriented programming concepts such as encapsulation, inheritance, and class methods. Each program defines a class with various attributes and methods, showcasing functionalities like calculating area and perimeter, managing item quantities, and handling student records. The document includes code snippets and their expected outputs for each program.

Uploaded by

poke.cool1000
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/ 11

21CSC101T Practice Program

Week – 4
-Pranshu Bharti
RA2311033010104
CINTEL AK – 1

PROGRAM 1:
#include <iostream>

using namespace std;

class Car

private:

int a_private = 10;

protected:

int a_protected = 15;

public:

int a_public = 5;

int getPrivate() { return a_private; }

int getProtected() { return a_protected; }

};

int main()

Car a;

cout << "a_public: " << a.a_public << endl;

cout << "a_protected: " << a.getProtected() << endl;

cout << "a_private: " << a.getPrivate() << endl;

return 0;

}
OUTPUT:
PROGRAM 2:
#include <iostream>

using namespace std;

class Rectangle

private:

int length, width;

public:

Rectangle(int l, int w)

length = l;

width = w;

int getArea()

return length * width;

int getPerimeter()

return 2*(length + width);

};

int main()

Rectangle r(100, 100);

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

cout << "Perimeter of r: " << r.getPerimeter() << endl;

return 0;

OUTPUT:
PROGRAM 3:

#include <iostream>

using namespace std;

class Vehicle

protected:

int speed = 180;

string colour = "Silver";

};

class Car:

public Vehicle {

int mfg_year;

bool pollution_check;

string serial_no;

public:

Car(int _mfg_year, bool _pollution_check, string _serial_no)

mfg_year = _mfg_year;

pollution_check = _pollution_check;

serial_no = _serial_no;

void showDetails()

cout << "Speed: " << speed << " km/h" << endl;

cout << "Colour: " << colour << endl;

cout << "Manufacturing Year: " << mfg_year << endl;

cout << "Pollution Check: " << (pollution_check ? "Valid" : "Not Valid") << endl;

cout << "Serial No: " << serial_no << endl;

}
};

int main()

Car car_a(2021, true, "WB42XY8890");

car_a.showDetails();

return 0;

OUTPUT:
PROGRAM 4:
#include <iostream>

using namespace std;

class Item

int quantity;

string name;

public:

Item(string n, int q) {

quantity = q;

name = n;

string getName() { return name; }

int getQty() { return quantity; }

int updateQty(int n) { quantity += n; }

};

int main()

Item orange("iPhone", 200);

Item apple("iPad", 80);

orange.updateQty(10);

apple.updateQty(5);

cout << orange.getName() << ": " << orange.getQty() << endl;

cout << apple.getName() << ": " << apple.getQty() << endl;

return 0;

OUTPUT:
PROGRAM 5:

#include <iostream>

using namespace std;

class Student

string name;

string reg_no;

int marks;

public:

Student() {};

Student(string n, string r, int m) {

name = n;

reg_no = r;

marks = m;

string getName() { return name; }

string getRegNo() { return reg_no; }

int getMarks() { return marks; }

};

class Batch {

Student *students;

int student_n;

public:

Batch(int s) { student_n = 0; students = new Student[s]; };

float averageMarks();

void addStudent(Student s);

};

float Batch::averageMarks() {

float sum = 0;

for (int i = 0; i < student_n; i++) {


sum += students[i].getMarks();

return sum/student_n;

void Batch::addStudent(Student s) {

students[student_n] = s;

student_n++;

int main()

Batch AK1(70);

Student PB("Pranshu Bharti", "104", 100);

Student AS("Arjun Shrinet", "102", 95);

Student VS("Vishnu Singh", "076", 90);

AK1.addStudent(PB);

AK1.addStudent(AS);

AK1.addStudent(VS);

cout << "Batch Average: " << AK1.averageMarks() << endl;;

return 0;

OUTPUT:
PROBLEM 6:

PROBLEM 7:
PROBLEM 8:

PROBLEM 9:
PROBLEM 10:

You might also like