Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Object Oriented Programming Lab 5

Uploaded by

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

Object Oriented Programming Lab 5

Uploaded by

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

Object Oriented Programming Lab

Lab Manual (Lab 5)

School of Systems and Technology


UMT Lahore Pakistan
Objective:
The objective of this lab is to cover:

 Encapsulation (Getter and Setter Functions)


 Objects as Data members
 Objects as Function Arguments & Return Types

C++ Access Specifiers


Access Specifiers
By now, you are quite familiar with the public keyword that appears in all of our class examples:

In C++, there are three access specifiers:

 public - members are accessible from outside the class


 private - members cannot be accessed (or viewed) from outside the class
 protected - members cannot be accessed from outside the class, however, they can be
accessed in inherited classes. You will learn more about Inheritance later.

C++ Encapsulation
Encapsulation

The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To
achieve this, you must declare class variables/attributes as private (cannot be accessed from
outside the class). If you want others to read or modify the value of a private member, you can
provide public get and set methods.

Access Private Members


To access a private attribute, use public "get" and "set" methods:

Sample Code:

#include <iostream>
using namespace std;

class Employee {
private:
// Private attribute
int salary;
public:
// Setter
void setSalary(int s) {
salary = s;
}
// Getter
int getSalary() {
return salary;
}
};

int main() {
Employee myObj;
myObj.setSalary(50000);
cout << myObj.getSalary();
return 0;

Output:

Example explained

The salary attribute is private, which have restricted access.


The public setSalary() method takes a parameter (s) and assigns it to the salary attribute (salary
= s).
The public getSalary() method returns the value of the private salary attribute.
Inside main(), we create an object of the Employee class. Now we can use
the setSalary() method to set the value of the private attribute to 50000. Then we call
the getSalary() method on the object to return the value.

Accessing data members


The data members and member functions of class can be accessed using the dot (‘.’) operator
with the object. For example if the name of object is obj and you want to access the member
function with the name printName() then you will have to write obj.printName() .

Sample Code:

// C++ program to demonstrate


// accessing of data members

#include <bits/stdc++.h>
using namespace std;
class Geeks
{
// Access specifier
public:

// Data Members
string geekname;

// Member Functions()
void printname()
{
cout << "Geekname is: " << geekname;
}
};

int main() {

// Declare an object of class geeks


Geeks obj1;

// accessing data member


obj1.geekname = "Abhi";

// accessing member function


obj1.printname();
return 0;}
Output:

Objects as function arguments & return types

Passing an Object as argument


To pass an object as an argument we write the object name as the argument while calling the
function the same way we do it for other variables.

Syntax:

function_name(object_name);

Sample Code:

#include <iostream>
using namespace std;

class Student {

public:
double marks;

// constructor to initialize marks

};

// function that has objects as parameters


void calculateAverage(Student s1, Student s2) {

// calculate the average of marks of s1 and s2


double average = (s1.marks + s2.marks) / 2;

cout << "Average Marks = " << average << endl;

int main() {
Student student1, student2(56.0);
Student1.marks=88.0;

// pass the objects as arguments


calculateAverage(student1, student2);

return 0;
}

Output:

Sample Code:

C++ Return Object from a Function


#include <iostream>
using namespace std;

class Student {
public:
double marks1, marks2;

// function that returns object of Student


Student createStudent() {
Student student;

// Initialize member variables of Student


student.marks1 = 96.5;
student.marks2 = 75.0;

// print member variables of Student


cout << "Marks 1 = " << student.marks1 << endl;
cout << "Marks 2 = " << student.marks2 << endl;

return student;
}
};

int main() {
Student student1;

// Call function
student1 =student1.createStudent();

return 0;
}

Output:
TASKS:
TASK 1:
Create a class named ‘Student’ that has the following private data members:
1. course1_gpa
2. course2_gpa
3. course3_gpa
And the following member functions:
1. Student (){}
2. Student (float cg1, float cg2, float cg3) {}
3. void calculateCGPA (Student s){}

(course1_gpa*3 + course2_gpa*3 + course3_gpa*3) / (3+3+3)

Calculate & display the final CGPA for one Student using the above formula.

TASK 2:
Create a class named ‘Employee’ that has the following private data members:
1. salary
2. hours_worked
3. total_hours (N), N = 40 hours/week
4. salary_per_hour (N), N =1500
And the following member functions:

1. Employee (){}
2. Employee (float hw, float th, float sph) {}
3. Employee calculateSalary (){}

overtime = total_hours - hours_worked


salary = (overtime * salary_per_hour)

Calculate & return the overtime salary for one Employee.

You might also like