Professional Documents
Culture Documents
DownloadClassSessionFile 11
DownloadClassSessionFile 11
Computer Lab.
EXERCISE 1: C++ STRUCTURES
Create a C++ structure named "Person" with the
following members:
1. Name (string)
2. Age (int)
Write a program that declares an instance of the
"Person" structure, initializes its members, and then
prints the person's name and age.
#include <iostream>
#include <string>
struct Person {
std::string name;
int age;
};
int main() {
Person person;
person.name = "John Doe";
person.age = 30;
std::cout << "Name: " << person.name << std::endl;
std::cout << "Age: " << person.age << std::endl;
return 0;
}
EXPLANATION:
In this code, we define a structure named "Person" with two members: name (a
string) and age (an integer). In the main function, we create an instance of the
"Person" structure, initialize its members, and then print the name and age using
std::cout.
C++ INHERITANCE
Create a base class named "Animal" with a member variable "name" (string). Create
a derived class named "Dog" that inherits from "Animal" and adds a member
variable "breed" (string). Create another derived class named "Cat" that also inherits
from "Animal" and adds a member variable "color" (string).
Write a program that creates instances of the "Dog" and "Cat" classes, sets their
respective member variables, and prints the name, breed (for the dog), and color (for
the cat).
#include <iostream>
#include <string>
class Animal {
protected:
std::string name;
public:
Animal(const std::string& n) : name(n) {}
};
class Dog : public Animal {
private:
std::string breed;
public:
Dog(const std::string& n, const std::string& b) : Animal(n), breed(b) {}
void displayInfo() {
std::cout << "Name: " << name << std::endl;
std::cout << "Breed: " << breed << std::endl;
}
};
class Cat : public Animal {
private:
std::string color;
public:
Cat(const std::string& n, const std::string& c) : Animal(n), color(c) {}
void displayInfo() {
std::cout << "Name: " << name << std::endl;
std::cout << "Color: " << color << std::endl;
}
};
int main() {
Dog myDog("Buddy", "Golden Retriever");
Cat myCat("Whiskers", "Gray");
myDog.displayInfo();
myCat.displayInfo();
return 0;
}
EXPLANATION:
In this code, we define a base class "Animal" with a name member. Two derived
classes, "Dog" and "Cat," inherit from the "Animal" class and add their own specific
member variables (breed and color, respectively). In the main function, we create
instances of the "Dog" and "Cat" classes, set their properties, and use member
1. x-coordinate (int)
2. y-coordinate (int)
Write a program that declares an array of three "Point" structures, initializes their
coordinates, and then finds and prints the point with the highest y-coordinate.
#include <iostream>
struct Point {
int x;
int y;
};
int main() {
Point points[3];
points[0] = {1, 3};
points[1] = {2, 5};
points[2] = {4, 2};
Point highestY = points[0];
for (int i = 1; i < 3; ++i) {
if (points[i].y > highestY.y) {
highestY = points[i];
}
}
std::cout << "Point with highest y-coordinate: (" << highestY.x << ", " << highestY.y << ")" << std::endl;
return 0;
}
EXPLANATION:
declare an array of three "Point" structures, initialize their coordinates, and then find
the point with the highest y-coordinate by iterating through the array and comparing
the y-coordinates.
C++ CLASSES
Create a C++ class named "Student" with the following members:
1. Name (string)
2. Roll Number (int)
3. Grade (char)
Include member functions to: Display student information.
Write a program that creates an array of two "Student" objects, sets their attributes,
and displays the information of both students.
#include <iostream>
#include <string>
class Student {
private:
std::string name;
int rollNumber;
char grade;
public:
void displayInfo() {
};
int main() {
Student students[2] = {
Student("Alice", 101, 'A'),
Student("Bob", 102, 'B')
};
return 0;
}
EXPLANATION:
In this exercise, we create a "Student" class with private members for name, roll
number, and grade. The constructor initializes these values, and we have a member
objects, set their attributes, and use a loop to display the information of both
students.
C++ INHERITANCE
Create a base class named "Vehicle" with a member variable "brand" (string). Create
a derived class named "Car" that inherits from "Vehicle" and adds a member variable
"model" (string). Create another derived class named "Bike" that also inherits from
"Vehicle" and adds a member variable "wheelSize" (int).
Write a program that creates instances of the "Car" and "Bike" classes, sets their
respective member variables, and prints the brand and additional details (model for
cars and wheel size for bikes).
#include <iostream>
#include <string>
class Vehicle {
protected:
std::string brand;
public:
Vehicle(const std::string& b) : brand(b) {}
};
class Car : public Vehicle {
private:
std::string model;
public:
Car(const std::string& b, const std::string& m) : Vehicle(b), model(m) {}
void displayInfo() {
std::cout << "Brand: " << brand << std::endl;
std::cout << "Model: " << model << std::endl;
}
};
class Bike : public Vehicle {
private:
int wheelSize;
public:
Bike(const std::string& b, int w) : Vehicle(b), wheelSize(w) {}
void displayInfo() {
std::cout << "Brand: " << brand << std::endl;
std::cout << "Wheel Size: " << wheelSize << " inches" << std::endl;
}
};
int main() {
Car myCar("Toyota", "Camry");
Bike myBike("Schwinn", 26);
myCar.displayInfo();
std::cout << std::endl;
myBike.displayInfo();
return 0;
}
EXPLANATION:
In this exercise, we create a base class "Vehicle" with a brand member. Two derived
classes, "Car" and "Bike," inherit from the base class and add their specific members
(model and wheelSize, respectively). In the main function, we create instances of the
"Car" and "Bike" classes, set their properties, and use member functions to display