0% found this document useful (0 votes)
7 views

Lab 03 Manual

This document outlines the lab instructions for COMP-112L, focusing on C++ class implementation. It covers topics such as constant and non-constant functions, data members, and data encapsulation with examples. Students are required to submit their reports with specific naming conventions and are warned about penalties for late submissions.

Uploaded by

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

Lab 03 Manual

This document outlines the lab instructions for COMP-112L, focusing on C++ class implementation. It covers topics such as constant and non-constant functions, data members, and data encapsulation with examples. Students are required to submit their reports with specific naming conventions and are warned about penalties for late submissions.

Uploaded by

mobeenkiani36
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Object Oriented Programming Lab 03

Course Code: COMP-112L Class CYS (G) + CYS(B) + AI(O)


Lab Engineer Laiba Khalid Semester 2nd
Lab Title C++ Classes Section -
Name Reg no.
Content Covered C++ Class Implementation.
Instructions:

• Submit the file with your names following your registration numbers like AI001_Name.
• Submit soft copy of the report before deadline. Marks will be deducted for late
submissions.

Constant and Non-Constant Functions, Data Members, and Data


Encapsulation

Topic 1: Constant and Non-Constant Functions

Theory

 A constant function does not modify any member variables of a class.


 Declared using the const keyword at the end of the function declaration.
 A non-constant function can modify member variables.

Example

#include <iostream>

using namespace std;

class Sample {

private:

int value;

public:

Sample(int v) : value(v) {}
void show() const { // Constant function

cout << "Value: " << value << endl;

void setValue(int v) { // Non-constant function

value = v;

};

int main() {

Sample obj(10);

obj.show();

obj.setValue(20);

obj.show();

return 0;

Topic 2: Constant and Non-Constant Data Members

Theory

 A constant data member must be initialized at declaration or in the constructor.


 A non-constant data member can be modified anytime.

Example:

#include <iostream>

using namespace std;


class Student {

private:

const int rollNo;

string name;

public:

Student(int r, string n) : rollNo(r), name(n) {}

void show() const {

cout << "Roll No: " << rollNo << ", Name: " << name << endl;

};

int main() {

Student s1(101, "Alice");

s1.show();

return 0;

Topic 3: Data Encapsulation

Theory

 Encapsulation is bundling data and methods together.


 Uses private or protected access specifiers to restrict direct access to data members.
 Provides setter and getter methods to access private members.

Example:

#include <iostream>

using namespace std;


class BankAccount {

private:

double balance;

public:

BankAccount(double b) : balance(b) {}

void deposit(double amount) {

balance += amount;

double getBalance() const {

return balance;

};

int main() {

BankAccount account(500.0);

account.deposit(200);

cout << "Balance: " << account.getBalance() << endl;

return 0;

You might also like