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

Inheritance

The document contains C++ code demonstrating the use of classes and inheritance. It includes a 'Student' class for managing student information, a 'Degree' class placeholder, and a 'Base' class with a 'Subclass' that overrides a method. The main function in each section creates instances and displays their information.

Uploaded by

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

Inheritance

The document contains C++ code demonstrating the use of classes and inheritance. It includes a 'Student' class for managing student information, a 'Degree' class placeholder, and a 'Base' class with a 'Subclass' that overrides a method. The main function in each section creates instances and displays their information.

Uploaded by

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

Ahmed Raza (104)

// INHERITANCE

// question no.1

#include <iostream>

#include <string>

using namespace std;

class Student {

private:

string name;

int age;

string address;

public:

Student() : name("unknown"), age(0), address("not available") {}

void setInfo(string newName, int newAge) {

name = newName;

age = newAge;

void setInfo(string newName, int newAge, string newAddress) {

name = newName;

age = newAge;

address = newAddress;

void printInfo() {

cout << "Name: " << name << endl;


cout << "Age: " << age << endl;

cout << "Address: " << address << endl;

};

int main() {

const int numStudents = 10;

Student students[numStudents];

students[0].setInfo("Alice", 20, "123 Main St");

students[1].setInfo("Bob", 22, "456 Elm St");

students[2].setInfo("Charlie", 19, "789 Oak St");

students[3].setInfo("David", 21, "321 Pine St");

students[4].setInfo("Eva", 23, "654 Cedar St");

students[5].setInfo("Frank", 20, "987 Maple St");

students[6].setInfo("Grace", 18, "135 Birch St");

students[7].setInfo("Hannah", 22, "468 Walnut St");

students[8].setInfo("Ian", 19, "246 Spruce St");

students[9].setInfo("Jenny", 21, "579 Ash St");

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

cout << "Student " << i+1 << ":\n";

students[i].printInfo();

cout << endl;

return 0;

// question no.2

#include <iostream>

using namespace std

class Degree {

public:
cout << "\nUndergraduate: ";

undergraduate.getDegree();

cout << "\nPostgraduate: ";

postgraduate.getDegree();

return 0;

// question no.3

#include <iostream>

using namespace std;

class Base {

protected:

int i;

public:

Base(int val_i) : i(val_i) {}

void printNum() {

cout << "Value of i: " << i << endl;

};

class Subclass : public Base {

private:

int j;

public:

Subclass(int val_i, int val_j) : Base(val_i), j(val_j) {}

void printNum() {

cout << "Value of j: " << j << endl;

}
};

int main() {

Subclass obj(5, 10);

obj.printNum();

obj.Base::printNum();

return 0;

You might also like