0% found this document useful (0 votes)
33 views23 pages

Lecture 05 OOP

this is oop lec 5

Uploaded by

hi070125f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views23 pages

Lecture 05 OOP

this is oop lec 5

Uploaded by

hi070125f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Object Oriented Programming

(OOP)

Lecture 05

Engr. Dr. Ghulam Fareed Laghari

Department of Computer Science


Barani Institute of Management Sciences (BIMS)
Rawalpindi
2

CONTENT
 Constructors: Example Problems
 Problem#1

 Problem#2

 Problem#3

 Problem#4

 Problem#5
3

Constructors: Example Problems


 PROBLEM 1:
Write a program that defines a class Student with the following data members:

 Name (string)

 Age (int)

 Grade (char)

The class should have a default constructor that initializes these data members with default values:

 Name: "Unknown"

 Age: 0

 Grade: 'U'

In main(), create two objects:

1. One using the default constructor.

2. Another with manually assigned values (using member functions).

Display the details of both students using a member function displayDetails().


4

Constructors: Example Problems


#include <iostream> // Member function to display details
using namespace std; void displayDetails() {
cout << "Name: " << name << ", Age: " << age << ",
class Student { Grade: " << grade << endl; } };
string name; int main() {
int age; Student student1; // Default constructor
char grade; Student student2; // Another object

public: // Default constructor student2.setDetails("Salman", 19, 'B+');


Student() {
name = "Unknown"; cout << "Student 1 Details: ";
age = 0; student1.displayDetails();
grade = 'U'; }
cout << "Student 2 Details: ";
// Member function to set details student2.displayDetails();
void setDetails(string n, int a, char g) {
name = n; return 0;
age = a; }
grade = g; }
5

Constructors: Example Problems


 The output of the code would be:

 Student 1 Details: Name: Unknown, Age: 0, Grade: U

 Student 2 Details: Name: Salman, Age: 19, Grade: B+


6

Constructors: Example Problems


 Code Explanation:
 This code defines a simple Student class with three attributes: name, age, and grade. It includes:

1. Default Constructor: This initializes the name as "Unknown", age as 0, and grade as 'U'.

2. setDetails Function: This allows setting the values for name, age, and grade.

3. displayDetails Function: This outputs the student's information to the screen.

 Two student objects are created: student1 and student2.

1. student1 uses the default constructor, so its details are initialized to default values.

2. student2 has its details set explicitly using the setDetails function.

 The details of both students are then displayed.


7

Constructors: Example Problems


PROBLEM 2:

Write a program to define a class Rectangle with the following data members:

Length (float)

Breadth (float)

The class should have a parameterized constructor that initializes the length and breadth.
Also, write a member function area() to calculate the area of the rectangle.

In main(), create two objects using the parameterized constructor with different values and
print their areas.
8

Constructors: Example Problems


#include <iostream> int main() {
using namespace std;
Rectangle rect1(4.5, 3.2);
class Rectangle {
Rectangle rect2(6.0, 5.0);
float length;
float breadth;

public: cout << "Area of rect1: " << rect1.area() << endl;
// Parameterized constructor cout << "Area of rect2: " << rect2.area() << endl;
Rectangle(float l, float b) {
length = l;
breadth = b; }
return 0;
// Function to calculate area }
float area() {
return length * breadth; } };
9

Constructors: Example Problems


 The output of the code would be:

Area of rect1: 14.4


Area of rect2: 30
10

Constructors: Example Problems


 Code Explanation:
 This code defines a Rectangle class to represent a rectangle and calculates its area.
 Attributes: 1) length: Holds the length of the rectangle. 2) breadth: Holds the breadth of the
rectangle.
 Parameterized Constructor: The constructor takes two arguments, l and b, to initialize the
rectangle's length and breadth.
 area() Function: This function computes and returns the area of the rectangle by multiplying
the length and breadth.
 Two objects rect1 and rect2 are created with different dimensions.
 The area of each rectangle is calculated using the area() function and printed to the output.
11

Constructors: Example Problems


 PROBLEM 3:

 Write a class Box with the following:

Width (float)

Height (float)

Depth (float)

 The class should have:

A default constructor that initializes all values to 1.

A parameterized constructor that takes three arguments and initializes the width, height, and depth.

A constructor with one parameter that initializes a cube (all sides equal).

 In main(), create three different objects using the different constructors, and write a function to calculate the volume of
the box. Display the volume of each box.
12

Constructors: Example Problems


#include <iostream> // Function to calculate volume
using namespace std; float volume() {
return width * height * depth; } };
class Box {
float width, height, depth; int main() {
Box box1; // Default constructor
public:
// Default constructor Box box2(2.5, 3.0, 4.0); // Parameterized constructor
Box() { width = height = depth = 1.0; } Box box3(3.0); // Cube constructor

// Parameterized constructor
Box(float w, float h, float d) { cout << "Volume of box1: " << box1.volume() << endl;
width = w; cout << "Volume of box2: " << box2.volume() << endl;
height = h;
depth = d; } cout << "Volume of box3 (cube): " << box3.volume() <<
endl;
// Constructor for cube
Box(float side) {
return 0;
width = height = depth = side; }
}
13

Constructors: Example Problems


 The output of the code would be:

Volume of box1: 1
Volume of box2: 30
Volume of box3 (cube): 27
14

Constructors: Example Problems


 Code Explanation:
 This code defines a Box class that can represent both rectangular boxes and cubes, allowing the calculation of their
volumes.
 Attributes: width, height, depth represent the dimensions of the box.
 Constructors:
1. Default Constructor: Initializes all dimensions (width, height, depth) to 1.0, representing a default box.
2. Parameterized Constructor: Takes w, h, and d to initialize the width, height, and depth, allowing for a custom
rectangular box.
3. Cube Constructor: Takes a single side value and sets all dimensions equal, representing a cube.
 volume() Function: This function calculates the volume of the box by multiplying width * height * depth.
 Three Box objects are created: 1) box1 is created using the default constructor. 2) box2 is created using the
parameterized constructor with specified dimensions. 3) box3 is created using the cube constructor with all sides set to 3.
 The volume of each box is calculated using the volume() function and printed.
15

Constructors: Example Problems


 PROBLEM 4:

 Define a class Book with the following data members:

Title (string)

Author (string)

Price (float)

 The class should have:

A parameterized constructor to initialize the attributes.

A copy constructor that copies the values of another book.

 In main(), create one object using the parameterized constructor and another using the copy constructor.

 Display the details of both objects using a member function.


16

Constructors: Example Problems


#include <iostream> // Function to display details
using namespace std; void displayDetails() {
cout << "Title: " << title << ", Author: " << author <<
class Book { ", Price: $" << price << endl; } };
string title;
string author; int main() {
float price; Book book1("C++ Programming", “Robert Lafore",
29.99);
public: // Parameterized constructor Book book2 = book1; // Copy constructor
Book(string t, string a, float p) {
title = t; cout << "Book 1 Details: ";
author = a; book1.displayDetails();
price = p; }
cout << "Book 2 Details (Copied): ";
// Copy constructor book2.displayDetails();
Book(const Book &obj) {
title = obj.title; return 0; }
author = obj.author;
price = obj.price; }
17

Constructors: Example Problems


 The output of the code would be:

Book 1 Details: Title: C++ Programming, Author: Robert Lafore,


Price: $29.99
Book 2 Details (Copied): Title: C++ Programming, Author: Robert
Lafore, Price: $29.99
18

Constructors: Example Problems


 Code Explanation:
 This code defines a Book class to represent a book with a title, author, and price. It demonstrates the use of both a
parameterized constructor and a copy constructor.
 Attributes: title: Holds the title of the book. author: Holds the author's name. price: Holds the price of the book.
 Parameterized Constructor: Initializes a Book object with the values passed for title, author, and price.
 Copy Constructor: Creates a copy of an existing Book object by copying its title, author, and price.
 displayDetails() Function: Outputs the details of the book (title, author, price) to the screen.
 A Book object book1 is created using the parameterized constructor.
 A second Book object book2 is created using the copy constructor, which copies the attributes of book1.
 The details of both book1 and book2 are displayed using the displayDetails() function.
19

Constructors: Example Problems


 PROBLEM 5:

 Write a program that defines a class Account with the following attributes:

 Account Number (int)

 Account Holder Name (string)

 Balance (float)

 The class should have a parameterized constructor with default arguments. If the account holder's name is not provided, it should be
initialized to "Unknown", and the balance should default to 0.

 In main(), create three objects:

 One with all arguments provided.

 One with only the account number and balance.

 One with only the account number.

 Display the account details for each object using a member function.
20

Constructors: Example Problems


#include <iostream> int main() {
using namespace std;
Account acc1(101, “Akram", 500.0); // All arguments
class Account { Account acc2(102, “Bilal"); // Default balance
int accNumber; Account acc3(103); // Default name and balance
string accHolderName;
float balance;
cout << "Account 1: ";
public: // Constructor with default arguments
Account(int acc, string name = "Unknown", float bal = acc1.displayAccount();
0.0) { accNumber = acc;
accHolderName = name;
cout << "Account 2: ";
balance = bal; }
acc2.displayAccount();
// Function to display account details
void displayAccount() {
cout << "Account Number: " << accNumber cout << "Account 3: ";
<< ", Account Holder: " << accHolderName acc3.displayAccount();
<< ", Balance: $" << balance << endl; } };
return 0; }
21

Constructors: Example Problems


 The output of the code would be:

Account 1: Account Number: 101, Account Holder: Akram, Balance: $500

Account 2: Account Number: 102, Account Holder: Bilal, Balance: $0

Account 3: Account Number: 103, Account Holder: Unknown, Balance: $0


22

Constructors: Example Problems


 Code Explanation:
 This code defines an Account class representing a bank account. It utilizes a constructor with default arguments to demonstrate
different ways of initializing an object.
 Attributes:
 accNumber: Holds the account number.
 accHolderName: Holds the name of the account holder.
 balance: Holds the account balance.
 Constructor with Default Arguments: The constructor takes three arguments: acc, name, and bal, with default values for name
as "Unknown" and bal as 0.0. This allows for flexibility in object creation, depending on how many arguments are provided.
 displayAccount() Function: Displays the account details, including account number, holder's name, and balance.
 Three Account objects are created: 1) acc1 is created with all three arguments (account number, name, and balance). 2) acc2 is
created with a default balance (no value for bal provided). 3) acc3 is created with both default name and balance (only account
number provided). The account details are displayed for all three objects using the displayAccount() function.
Thank You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy