0% found this document useful (0 votes)
80 views14 pages

Lab Report 3

This document is a lab report for an object oriented programming class. It discusses class scope and accessing class members. The lab objectives are to teach class data member scope and rules for accessing class data members and member functions. At the end of the lab, students will understand how to access class data members and member functions. Examples are provided to illustrate public and private access scopes for class data and methods. Tasks are included for students to code examples and check for errors when accessing private members from outside the class.

Uploaded by

Khizer Hayat
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)
80 views14 pages

Lab Report 3

This document is a lab report for an object oriented programming class. It discusses class scope and accessing class members. The lab objectives are to teach class data member scope and rules for accessing class data members and member functions. At the end of the lab, students will understand how to access class data members and member functions. Examples are provided to illustrate public and private access scopes for class data and methods. Tasks are included for students to code examples and check for errors when accessing private members from outside the class.

Uploaded by

Khizer Hayat
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/ 14

CSC241 Object Oriented Programming Fall 2020

CSC241
Object Oriented Programming

Lab # 03

Name Abdul Mobeen

Registration Number FA19-BEE-028

Class BEE-3A

Instructor's Name Dr Sajid Ali

CO MSATS Un ive rsity Islamab ad Pa ge 1|


14
CSC241 Object Oriented Programming Fall 2020

Lab 03
Class Scope and Accessing Class Members

1. Objectives
The objective of this lab is to teach the students, the scope of class data members and its member
functions.

2. Outcome
At the end of this lab student will be familiar with the accessing rules of class data members and
member functions

3. Introduction
In object-oriented programming, methods and variables have various scope. Scope means that
the method or variable may or may not be directly accessible to other objects or classes. Classes
that do not have instances may be accessible to the system.

One of the techniques in object-oriented programming is encapsulation. It concerns the hiding of


data in a class and making them available only through its methods. In this way the chance of
making accidental mistakes in changing values is minimized. C++ allows you to control access
to classes, methods, and fields via so-called access modifiers. The access to classes, constructors,
methods and fields are regulated using access modifiers
i.e. a class can control what information or data can be accessible by other classes. To take
advantage of encapsulation, you should minimize access whenever possible.

3.1. Class Scope


Class variables and class methods are associated with a class. An instance of the class (object) is
not required to use these variables or methods. Class methods cannot access instance variables or
methods, only class variables and methods.

3.2. Instance Scope


Instance variables and instance methods are associated with a specific object. They can access
class variables and methods.

3.3. Private Scope


Private variables and private methods are only accessible to the object they are contained in.

3.4. Protected Scope


Protected variables and protected methods are accessible by the class they are in and inheriting
classes (sub classes) only.

CO MSATS Un ive rsity Islamab ad Pa ge 2|


14
CSC241 Object Oriented Programming Fall 2020

3.5. Public Scope


Public variables and public methods are accessible outside the object they are contained in. They
are accessible to any other object.

3.6. Encapsulation
The process of providing a public interface to interact with the object while hiding other
information inside the object is called encapsulation.

4. Examples
The following program illustrates the usage of objects and classes in C++:
// The program uses public member functions to input two private numbers, add two private
numbers and display the result

#include<iostream>
using namespace std;

class add //Specifies the class


{
private:
int iNum1, iNum2, iNum3; //Member data

public:
void input(int iVar1, int iVar2) //Member function
{
cout<<"Functions to assign values to the member data"<<endl;
iNum1 = iVar1;
iNum2 = iVar2;
}
void sum (void) //Member function
{
cout<<"Functions to find the sum of two numbers"<<endl;
iNum3 = iNum1+iNum2;
}
void disp(void) //Member function
{
cout<<"The sum of the two numbers is "<<iNum3<<endl;
}
};

CO MSATS Un ive rsity Islamab ad Pa ge 3|


14
CSC241 Object Oriented Programming Fall 2020

/////////main function of the program///////////

int main()
{
add A1;
int iX, iY;
cout<<"Input two numbers"<<endl;
cin>>iX;
cin>>iY;
A1.input(iX, iY);
A1.sum();
A1.disp();
system("pause");
return 0 ;
}

Output:

CO MSATS Un ive rsity Islamab ad Pa ge 4|


14
CSC241 Object Oriented Programming Fall 2020

InLab Tasks

5.1. Code the example given above and check the errors if you try to access the private data
members in main() function.

Code:
#include <iostream>

using namespace std;

class add

private:

int inum1,inum2,inum3;

void input(int var1,int var2)

cout<<"function to assign values"<<endl;

inum1=var1;

inum2=var2;

} void sum(void)

cout<<"function to find sum of two numbers"<<endl;

inum3=inum1+inum2;

void disp(void)

cout<<"the sum of two numvers is"<<endl;

cout<<inum3;

CO MSATS Un ive rsity Islamab ad Pa ge 5|


14
CSC241 Object Oriented Programming Fall 2020

};

int main()

add A1;

int iX,iY;

cout<<"input two numbers\n";

cin>>iX;

cin>>iY;

A1.input(iX,iY);

A1.disp();

return 0;

ERROR CHECK:

5.2. Modify the above task by making the scope of public member functions as private. Create
access functions in public scope to access private member functions from main().

Code:
#include <iostream>

using namespace std;

CO MSATS Un ive rsity Islamab ad Pa ge 6|


14
CSC241 Object Oriented Programming Fall 2020

class add

private:

int inum1,inum2,inum3;

void input(int var1,int var2)

cout<<"function to assign values"<<endl;

inum1=var1;

inum2=var2;

void sum(void)

cout<<"function to find sum of two numbers"<<endl;

inum3=inum1+inum2;

void disp(void)

cout<<"the sum of two numvers is"<<endl;

cout<<inum3;

public:

void in(int x,int y)

input(x,y);

void show()

CO MSATS Un ive rsity Islamab ad Pa ge 7|


14
CSC241 Object Oriented Programming Fall 2020

sum();

disp();

};

int main()

add A1;

int iX,iY;

cout<<"input two numbers\n";

cin>>iX;

cin>>iY;

A1.in(iX,iY);

A1.show();

return 0;

Output:

CO MSATS Un ive rsity Islamab ad Pa ge 8|


14
CSC241 Object Oriented Programming Fall 2020

5.3. Code the example given above and include a private constructor in the class. Create objects
of this class. Test the code and write down how the constructor will be called or unable to be
called?.

Code:
#include <iostream>
using namespace std;
class add
{
private:
int iNum1, iNum2, iNum3;
add(int a=0, int b=0)
{
iNum1=a;
iNum2=b;
}
public: void input(int iVar1, int iVar2)
{
cout<<"Functions to assign values to the member data"<<endl;
iNum1=iVar1; iNum2=iVar2;
}
void sum(void)
{
cout<<"Functions to find the sum of two numbers"<<endl;
iNum3=iNum1+iNum2;
}

CO MSATS Un ive rsity Islamab ad Pa ge 9|


14
CSC241 Object Oriented Programming Fall 2020

void disp(void)
{
cout<<"The sum of the two numbers is "<<iNum3<<endl;
} };
int main()
{
add A1;
int iX, iY;
cout<<"Input two numbers"<<endl;
cin>>iX;
cin>>iY;
A1.input(iX, iY);
A1.sum();
A1.disp();
return 0;
}

ERROR CHECK:

CO MSATS Un ive rsity Islamab ad P a g e 10 |


14
CSC241 Object Oriented Programming Fall 2020

Post Lab Tasks


6.1. Create a class of subtraction having two private data members. Create class methods to get
data from users and for subtraction of data members. Use appropriate access modifiers for class
methods.

Code:
#include <iostream>

using namespace std;

class subtraction

private:

int inum1,inum2,inum3;

void input(int var1,int var2)

inum1=var1;

inum2=var2;

void sum(void)

inum3=inum1-inum2;

void disp(void)

cout<<"the subtraction of two numvers is"<<endl;

cout<<inum3;

public:

CO MSATS Un ive rsity Islamab ad P a g e 11 |


14
CSC241 Object Oriented Programming Fall 2020

void in(int x,int y)

input(x,y);

void show()

sum();

disp();

}};

int main()

subtraction s1;

int iX,iY;

cout<<"input two numbers\n";

cin>>iX;

cin>>iY;

s1.in(iX,iY);

s1.show();

return 0; }

Output:

CO MSATS Un ive rsity Islamab ad P a g e 12 |


14
CSC241 Object Oriented Programming Fall 2020

7. References
[i] http://www.tutorialspoint.com/cplusplus/cpp_class_access_modifiers.htm
[ii] http://www.learncpp.com/cpp-tutorial/83-public-vs-private-access-specifiers/

Critical Analysis / Conclusion

In this lab we learnt about the scope of class data members and its member
functions. And now we are well familiar with the accessing rules of class data
members and member functions.

CO MSATS Un ive rsity Islamab ad P a g e 13 |


14
CSC241 Object Oriented Programming Fall 2020

Lab Assessment

Pre-Lab /1

In-Lab /5

/4 /10
Data Analysis

Data
Post-Lab /4 /4
Presentation

Writing Style /4

Instructor Signature and Comments

CO MSATS Un ive rsity Islamab ad P a g e 14 |


14

You might also like