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

Department of Computer Sciences: Bahria University, Lahore Campus

This document is a lab journal from Bahria University's Department of Computer Sciences for an Object Oriented Programming course. It details two programming tasks involving classes and objects in C++. The first task involves defining a Student class with data members for name, marks in three subjects, and a total mark calculation function. The second task involves defining a Batsman class with data members for code, name, batting statistics, and an average calculation function. Sample code is provided for both tasks.

Uploaded by

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

Department of Computer Sciences: Bahria University, Lahore Campus

This document is a lab journal from Bahria University's Department of Computer Sciences for an Object Oriented Programming course. It details two programming tasks involving classes and objects in C++. The first task involves defining a Student class with data members for name, marks in three subjects, and a total mark calculation function. The second task involves defining a Batsman class with data members for code, name, batting statistics, and an average calculation function. Sample code is provided for both tasks.

Uploaded by

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

Bahria University, Lahore Campus

Department of Computer Sciences


Lab Journal 03
(Fall 2020)

Course: Object Oriented Programming Date:


Course Code: CSC-210 Max Marks: 40
Lab Engineer:
Faculty’s Name: Aasim Ali
M. Tayyab Mir

Name: _________Ali haider___________________________ Enroll No: 03-134192-


060_______________________

Objectives:
The objective of this lab is to understand following concepts:

 Default Constructor
 Getters and Setters
Sample Code 01:
main.cpp

#include <iostream>
using namespace std;

class item
{
public:

void set(int enter_value) // Also known as setter used to set


user-defined values to private member variables. It usually
doesn’t have any return type.
{

keep_data = enter_value;

int get_value(void) //Also known as getter used to get private


member variables values set by the setter.
It has a return type of the type of respective
variable to be returned.
{

return keep_data;

}
private:
int keep_data;

};
%
Enrollment Number: ____________________________
int main()
{
item John_cat,Joe_cat,Big_cat;
John_cat.set(10);

Joe_cat.set(11);

Big_cat.set(12);

cout<<"Accessing data using class\n";


cout<<"-------------------------\n";
cout<<"Data value for John_cat is
"<<John_cat.get_value()<<"\n";
cout<<"Data value for Joe_cat is
"<<Joe_cat.get_value()<<"\n";
cout<<"Data value for Big_cat is
"<<Big_cat.get_value()<<"\n";
cout<<"\nAccessing data normally\n";
cout<<"---------------------------\n";

return 0;

Sample code 02:

main.cpp

#include<iostream>
using namespace std;
class rectangle
{
private:
int height;
int width;
public:
int area(void)
{
return (height * width);
}
void initialize(int initial_height, int initial_width)
{
height=initial_height;
width = initial_width;
}
};
int main()
{
rectangle wall;
wall.initialize(12,10);
cout<<"Area of the wall-->wall.area() = "<<wall.area()<<
"\n\n";
return 0;
}

Page 2 of 7
%
Enrollment Number: ____________________________

Lab Tasks:
Task1:

Define a class student with the following specifications:


Data Members
Name string
Eng, Math, Science float
Total float

Public Members
Take_data( ) - A function to accept values for Ali, Ahmed, eng, science and invoke
ctotal( ) to calculate total.
Show_data( ) - A function to display all the data members on the screen.
ctotal( ) - A function to calculate Eng + Math + Science with float return type.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
private:
int admno;
char sname[20];
float eng,math,science;
float total;
float ctotal()
{
return eng+math+science;
}
public:
void Takedata()
{
cout<<"Enter admission number ";
cin>> admno;
cout<<"Enter student name " ;
gets(sname);
cout<< "Enter marks in english, math, science ";
Page 3 of 7
%
Enrollment Number: ____________________________
cin>>eng>>math>>science;
total=ctotal();
}
void Showdata()
{
cout<<"Admission number "<<admno<<"\nStudent name "<<sname<<"\nEnglish "
<<eng<<"\nMath "<<math<<"\nScience "<<science<<"\nTotal "<<total;
}
};
int main ()
{
clrscr();
student obj ;
obj.Takedata();
obj.Showdata();
getch();
return 0;
}

Task2:

Define a class batsman with the following specifications:

Data Members
bcode 4 digits code number
bname string
innings, notout, runs integer type
batavg - it is calculated according to the formula -- batavg =runs/(innings-notout)

Public Members
readdata( ) Function to accept value from bcode, name, innings, notout and
invoke the function calcavg( )
calcavg( ) Function to compute batavg
displaydata( ) Function to display the data members on the screen.

#include<iostream.h>

#include<conio.h>
Page 4 of 7
%
Enrollment Number: ____________________________

#include<stdio.h>

class batsman

int bcode;

char bname[20];

int innings,notout,runs;

int batavg;

void calcavg()

batavg=runs/(innings-notout);

public :

void readdata ();

void displaydata();

};

Page 5 of 7
%
Enrollment Number: ____________________________
void batsman::readdata ()

cout<<"Enter batsman code ";

cin>> bcode;

cout<<"Enter batsman name ";

gets(bname);

cout<<"enter innings,notout and runs ";

cin>>innings>>notout>>runs;

calcavg();

void batsman::displaydata()

cout<<"Batsman code "<<bcode<<"\nBatsman name "<<bname<<"\nInnings


"<<innings

<<"\nNot out "<<notout<<"\nRuns "<<runs<<"\nBatting Average "<<batavg;

Page 6 of 7
%
Enrollment Number: ____________________________
int main()

batsman obj;

obj.readdata();

obj.displaydata();

getch();

return 0;

Page 7 of 7

You might also like