You are on page 1of 6

Data Structures and Algorithms Lab (Week 1)

Department of Computer Science


University of Management and Technology, Lahore

Instructor: CLO
 Mr. Shahbaz Qadeer CLO 1

Learning Objectives:
• Revision of object oriented programming concepts

Guidelines/Instructions:
• Use any compiler for C++
• Create meaningful variable names. Add comments for readability. Indent each line of your code.
• Plagiarism/Cheating is highly discouraged by penalizing to both who tried and one who shared his/her
code.

Today’s Task
• Setup C++ environment if already not setup.
• Practice sample programs.
• Submit the given tasks at the end of the lab.

In OOP, objects are instances of classes, which define their behavior and characteristics. Classes are like
blueprints for objects, providing a template for their creation. Classes encapsulate data and functions into a
single unit, which can then be used to create objects with specific attributes and behaviors.

The four main principles of OOP are:

Encapsulation: This refers to the practice of keeping data and the code that manipulates it together in a single
unit. This helps to ensure that the data is not accidentally modified by other parts of the program.

Abstraction: This involves representing complex real-world objects as simplified models in code. This makes it
easier to understand and work with these objects, as well as to modify and extend them as needed.

Inheritance: This allows new classes to be based on existing ones, inheriting their attributes and methods. This
can save time and effort when creating new classes that are similar to existing ones.

Polymorphism: This refers to the ability of objects to take on different forms or behave in different ways
depending on their context. This can be useful for creating flexible and reusable code.

Example 1: Animal Hierarchy Program (Inheritance)

C++ Program
#include <iostream>
#include <string> using
namespace std;
class Animal
{ protected:
string name;
string species;
public:
Animal(string name, string species)
{ this->name = name;
this->species = species;
}
void move() {
cout << name << " is moving." << endl;
}
}; class Mammal : public
Animal { public:
Mammal(string name, string species) : Animal(name, species) {}
void nurse() {
cout << name << " is nursing." << endl;
}
}; class Bird : public
Animal { public:
Bird(string name, string species) : Animal(name, species) {}
void fly() {
cout << name << " is flying." << endl;
}
};
int main() {
Mammal dog("Fido",
"Dog"); dog.move();
dog.nurse();

Bird penguin("Tux",
"Penguin"); penguin.move();
penguin.fly();

return 0;
}

Example 2: Polymorphism

C++ Program

Page 2 of 6
#include <iostream>
#include <vector> using
namespace std;
class Shape { public:
virtual void draw() {
cout << "Drawing a generic shape." << endl;
}
}; class Circle : public
Shape { public: void
draw() {
cout << "Drawing a circle." << endl;
}
}; class Square : public
Shape { public: void
draw() {
cout << "Drawing a square." << endl;
}
}; class Triangle : public
Shape { public: void
draw() {
cout << "Drawing a triangle." << endl;
}
};
int main() {

Circle circle;
Square square;
Triangle triangle;

circle.draw();
square.draw(); triangle.draw();

return 0;
}

Example 3: Abstraction

C++ Program
#include <iostream>
using namespace std;

class Animal { public:


virtual void makeSound() = 0;
}; class Dog : public
Animal { public: void
makeSound() {
cout << "Woof!" << endl;
}
}; class Cat : public
Animal { public: void
makeSound() {
cout << "Meow!" << endl;
}
};
int main() {
Animal* dog = new Dog();
Animal* cat = new Cat();
dog-
>makeSound();
cat->makeSound();

return 0;
}

This program defines an abstract Animal class with a pure virtual function makeSound(), which has no
implementation. Two derived classes, Dog and Cat, inherit from Animal and provide their own implementations
of makeSound().

In main(), the program creates instances of Dog and Cat and stores them as pointers to the base Animal class.
The program then calls the makeSound() method on each object, which demonstrates abstraction in action: the
base Animal class defines the interface for making a sound, but the actual implementation is left up to the
derived classes. By using the base class pointer to call the makeSound() method, the program doesn't need to
know or care about the specific implementation for each animal.

Example 4: Encapsulation

C++ Program

Page 4 of 6
#include <iostream>
#include <string> using
namespace std;
class Person {
private:
string name;
int age;

public: void setName(string


newName) { name =
newName;
} void setAge(int
newAge) { if (newAge
< 0) { age = 0;
} else {
age = newAge;
}
} string
getName()
{ return
name;
} int
getAge()
{ return
age;
}
}; int main() {
Person person;

person.setName("John Doe");
person.setAge(-10);
cout << "Name: " << person.getName() <<
endl; cout << "Age: " << person.getAge() <<
endl;

return 0;
}

This program defines a Person class with two private member variables, name and age. The class provides
public setter and getter methods to access these variables. The setName() and setAge() methods enforce
encapsulation by keeping the member variables private and controlling access to them through the public
methods.

In main(), the program creates an instance of the Person class and sets the name to "John Doe" and the age to
10. The setAge() method demonstrates encapsulation by validating the input value and ensuring that the age is
never negative. Finally, the program prints out the name and age of the person using the public getter methods.
By using these getter methods instead of accessing the member variables directly, the program maintains
encapsulation and prevents accidental modification of the private data.

Let’s do some practice using the concepts above.

1. Create a Vehicle base class with derived classes Car, Truck, and Motorcycle. Each derived class should
have its own properties and methods, but they should all inherit common properties and methods from the
Vehicle class.

2. Implement a program that manages a bank account. Use the Account class as the base class and create
derived classes for SavingsAccount and CheckingAccount. Implement polymorphism by allowing a user to
create and interact with different types of accounts through a common interface.

3. Create a program that simulates a game with different types of characters. Use the Character class as the
base class and create derived classes for PlayerCharacter and EnemyCharacter. Use encapsulation to control
access to character properties such as health, attack power, and defense.

4. Implement a program that manages a library. Use the Item class as the base class and create derived classes
for Book and DVD. Use inheritance to share common properties and methods between the derived classes,
and use abstraction to define a common interface for checking out and returning library items.

5. Create a program that simulates a zoo. Use the Animal class as the base class and create derived classes for
different types of animals such as Lion, Elephant, and Giraffe. Use encapsulation to protect the animal's
properties such as name, age, and species.

What to Submit

C++ Running file should be submitted on the provided link. Link will be provided latter.

Page 6 of 6

You might also like