OOP LAB Assignments

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Object Oriented Programming Lab

_____________________________________________________________________________________________________________________

Assignment No. 1
Title : :Install, Configure 64 bit Linux Operating Systems, study basic architecture, memory
system, and learn basic administration.

Learning Objective:
 To learn Linux installation process
 To study the Linux administrative commands

Learning Outcome:
Students are able to install Linux OS.
Students will be able to execute & use Linux administrative commands

Aim:
Install, Configure 64 bit Linux Operating Systems, study basic architecture, memory system, and
learn basic administration.

Theory:
Users Command
Users command displays currently logged in users. This command don’t have other parameters
other than help and version.
#user
tecmint
Who Command
who command simply return user name, date, time and host information. who command is
similar to w command. Unlike w command who doesn’t print what users are doing. Lets
illustrate and see the different between who and w commands.
# who
tecmint pts/0 2012-09-18 07:59 (192.168.50.1)

_____________________________________________________________________________________________
Department of Computer Engineering Page 1 DYPCOE, Akurdi, Pune
Object Oriented Programming Lab
_____________________________________________________________________________________________________________________

Whoami Command
whoami command print the name of current user. You can also use “who am i” command to
display the current user. If you are logged in as a root using sudo command “whoami” command
return root as current user. Use “who am i” command if you want to know the exact user logged
in.
# whoami
tecmint

ls Command
ls command display list of files in human readable format.
# ls -l
total 114
dr-xr-xr-x. 2 root root 4096 Sep 18 08:46 bin
dr-xr-xr-x. 5 root root 1024 Sep 8 15:49 boot

CP Command
Copy file from source to destination preserving same mode.
# cp -p fileA fileB

mv Command
Rename fileA to fileB. -i options prompt before overwrite. Ask for confirmation if exist already.
# mv -i fileA fileB
cat Command
cat command used to view multiple file at the same time.
# cat fileA fileB
Cd command (change directory)
with cd command (change directory) it will goes to fileA directory.
# cd /fileA

_____________________________________________________________________________________________
Department of Computer Engineering Page 2 DYPCOE, Akurdi, Pune
Object Oriented Programming Lab
_____________________________________________________________________________________________________________________

pwd command (print working directory)


pwd command return with present working directory.
# pwd
/root

Top Command
top command displays processor activity of your system and also displays tasks managed by
kernel in real-time. It’ll show processor and memory are being used. Use top command with ‘u‘
option this will display specific User process details as shown below. Press ‘O‘ (uppercase letter)
to sort as per desired by you. Press ‘q‘ to quit from top screen.
# top -u tecmint
top - 11:13:11 up 3:19, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 116 total, 1 running, 115 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.3%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1030800k total, 736188k used, 294612k free, 51760k buffers
Swap: 2064376k total, 0k used, 2064376k free, 547704k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1889 tecmint 20 0 11468 1648 920 S 0.0 0.2 0:00.59 sshd
1890 tecmint 20 0 5124 1668 1416 S 0.0 0.2 0:00.44 bash
6698 tecmint 20 0 11600 1668 924 S 0.0 0.2 0:01.19 sshd
6699 tecmint 20 0 5124 1596 1352 S 0.0 0.2 0:00.11 bash

kill command
Use kill command to terminate process. First find process id with ps command as shown below
and kill process with kill -9 command.
# ps -ef | grep init
root 1 0 0 07:53 ? 00:00:04 /sbin/init
root 7508 6825 0 11:48 pts/1 00:00:00 grep init
# kill- 9 7508

_____________________________________________________________________________________________
Department of Computer Engineering Page 3 DYPCOE, Akurdi, Pune
Object Oriented Programming Lab
_____________________________________________________________________________________________________________________

rm command
rm command used to remove or delete a file without prompting for confirmation.
# rm filename
Using -i option to get confirmation before removing it. Using options ‘-r‘ and ‘-f‘ will remove
the file forcefully without confirmation.

mkdir command example.


mkdir command is used to create directories under Linux.
# mkdir directoryname

Conclusion : Thus Linux administrative commands are executed.

_____________________________________________________________________________________________
Department of Computer Engineering Page 4 DYPCOE, Akurdi, Pune
Object Oriented Programming Lab
_____________________________________________________________________________________________________________________

Assignment No. 6

Title :: Personnel information system using constructor, destructor, static member functions,
friend class, this pointer, inline code and dynamic memory allocation.

Learning Objective:
To learn the concept of constructor, default constructor, copy, destructor, static member
functions, friend class, this pointer, inline code and dynamic memory allocation operators-new
and delete

Learning Outcome:
Students are able to implement program using different types of constructor, friend function,
friend class, static data member, static member function.

Aim:
Develop an object oriented program in C++ to create a database of student information system
containing the following information: Name, Roll number, Class, division, Date of Birth, Blood
group, Contact address, telephone number, driving license no. etc Construct the database with
suitable member functions for initializing and destroying the data viz constructor, default constructor,
Copy constructor, destructor, static member functions, friend class, this pointer, inline code and
dynamic memory allocation operators-new and delete.

Theory:
A special method of the class that will be automatically invoked when an instance of the class is
created is called as constructor. Following are the most useful features of constructor.
1) Constructor is used for Initializing the values to the data members of the Class.
2) Constructor is that whose name is same as name of class.
3) Constructor gets automatically called when an object of class is created.
4) Constructors never have a Return Type even void.

_____________________________________________________________________________________________
Department of Computer Engineering Page 5 DYPCOE, Akurdi, Pune
Object Oriented Programming Lab
_____________________________________________________________________________________________________________________

5) Constructor is of Default, Parameterized and Copy Constructors.


The various types of Constructor are as follows:-
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor

1. Default Constructor:- Default Constructor is also called as Empty Constructor which has no
arguments and It is Automatically called when we creates the object of class but Remember
name of Constructor is same as name of class and Constructor never declared with the help of
Return Type. Means we can‟t declare a Constructor with the help of void Return Type. , if we
never Pass or declare any Arguments then this called as the Copy Constructors.

2. Parameterized Constructor: - This is another type constructor which has some Arguments
and same name as class name but it uses some Arguments So For this We have to create object
of Class by passing some Arguments at the time of creating object with the name of class. When
we pass some Arguments to the Constructor then this will automatically pass the Arguments to
the Constructor and the values will retrieve by the Respective Data Members of the Class.

3. Copy Constructor: - This is also another type of Constructor. In this Constructor we pass the
object of class into the Another Object of Same Class. As name Suggests you Copy, means Copy
the values of one Object into the another Object of Class .This is used for Copying the values of
class object into an another object of class So we call them as Copy Constructor and For
Copying the values We have to pass the name of object whose values we wants to Copying and
When we are using or passing an Object to a Constructor then we must have to use the &
Ampersand or Address Operator.

Destructor: As we know that Constructor is that which is used for Assigning Some Values to
data Members and For Assigning Some Values this May also used Some Memory so that to free
up the Memory which is Allocated by Constructor, destructor is used which gets Automatically
Called at the End of Program and we doesn‟t have to Explicitly Call a Destructor and Destructor
_____________________________________________________________________________________________
Department of Computer Engineering Page 6 DYPCOE, Akurdi, Pune
Object Oriented Programming Lab
_____________________________________________________________________________________________________________________

Cant be Parameterized or a Copy This can be only one Means Default Destructor which Have no
Arguments. For Declaring a Destructor we have to use ~tiled Symbol in front of Destructor.

Static members
A class can contain static members, either data or functions. A static member variable has
following properties:
 It is initialized to zero when the first object of its class is created. No other initialization is
permitted.
 Only one copy of that member is created for the entire class and is shared by all the
objects of that class.
 It is the visible only within the class but its lifetime is the entire program.
Static data members of a class are also known as "class variables", because there is only one
unique value for all the objects of that same class. Their content is not different from one object
static members have the same properties as global variables but they enjoy class scope. For that
reason, and to avoid them to be declared several times, we can only include the prototype (its
declaration) in the class declaration but not its definition (its initialization). In order to initialize a
static data-member we must include a formal definition outside the class, in the global scope of
this class to another. Because it is a unique variable value for all the objects of the same class, it
can be referred to as a member of any object of that class or even directly by the class name (of
course this is only valid for static members.
A static member function has following properties
 A static function can have access to only other static members (fun or var) declared in the
same class
 A static function can be called using the class name instead of its object name
Class_name :: fun_name;
Static member functions are considered to have class scope. In contrast to non static member
functions, these functions have no implicit this argument; therefore, they can use only static data
members, enumerators, or nested types directly. Static member functions can be accessed
without using an object of the corresponding class type.

_____________________________________________________________________________________________
Department of Computer Engineering Page 7 DYPCOE, Akurdi, Pune
Object Oriented Programming Lab
_____________________________________________________________________________________________________________________

The following restrictions apply to such static functions:


1. They cannot access non static class member data using the member-selection operators (. or –
>).
2. They cannot be declared as virtual.
They cannot have the same name as a non static function that has the same argument
3. types.

E.g. // static members in classes


class StaticTest {
private: static int x; public:
static int count()
{ return x;
}
};
int StaticTest::x = 9;
int main()
{
printf_s("%d\n", StaticTest::count());
}
Output
9

Friend functions:
In principle, private and protected members of a class cannot be accessed from outside the same
class in which they are declared. However, this rule does not affect friends. Friends are functions
or classes declared as such. If we want to declare an external function as friend of a class, thus
allowing this function to have access to the private and protected members of this class, we do it
by declaring a prototype of this external function within the class, and preceding it with the
keyword friend.
Properties of friend function:
_____________________________________________________________________________________________
Department of Computer Engineering Page 8 DYPCOE, Akurdi, Pune
Object Oriented Programming Lab
_____________________________________________________________________________________________________________________

 It is not in the scope of the class to which it has been declared as friend.
 Since it is not in the scope of the class , it cannot be called using the object of that class
 It can be invoked like a normal function w/o the help of any object.
 It can be declared in private or in the public part of the class.
 Unlike member functions, it cannot access the member names directly and has to use an
object name and dot operator with each member name.

 friend functions

#include iostream>
using namespace std;
class CRectangle {
int width, height;
public:
void set_values (int, int);
int area () {return (width * height);} friend
CRectangle duplicate (CRectangle);
};
void CRectangle::set_values (int a, int b) {
width = a;
height = b;
}
CRectangle duplicate (CRectangle rectparam)
{
CRectangle rectres;
rectres.width = rectparam.width*2;
rectres.height = rectparam.height*2;
return (rectres);
}
int main ()
_____________________________________________________________________________________________
Department of Computer Engineering Page 9 DYPCOE, Akurdi, Pune
Object Oriented Programming Lab
_____________________________________________________________________________________________________________________

{ CRectangle rect, rectb;


rect.set_values (2,3);
rectb = duplicate (rect);
cout << rectb.area();
return 0;
}

The duplicate function is a friend of CRectangle. From within that function we have been able to
access the members width and height of different objects of type CRectangle, which are private
members. Notice that neither in the declaration of duplicate() nor in its later use in main() have
we considered duplicate a member of class CRectangle.

Friend classes
Just as we have the possibility to define a friend function, we can also define a class as friend of
another one, granting that second class access to the protected and private members of the first
one.

this pointer:
C++ uses a unique keyword called this to represent an object that invokes a member function.
this is a pointer that points to the object for which this function was called. This unique pointer is
automatically passed to a member function when it is called.

Important notes on this pointer:


 this pointer stores the address of the class instance, to enable pointer access ofthe
members to the member functions of the class.
 this pointer is not counted for calculating the size of the object.
 this pointers are not accessible for static member functions.
 this pointers are not modifiable.

_____________________________________________________________________________________________
Department of Computer Engineering Page 10 DYPCOE, Akurdi, Pune
Object Oriented Programming Lab
_____________________________________________________________________________________________________________________

Algorithm/Pseudo code:
1. Start
2. Read personnel information such as Name, Date of Birth, Blood group, Height, Weight,
Insurance Policy, number, Contact address, telephone number, driving license no..
3. Print all information from database.
4. Stop
Input:
Student information such as Name, Roll number, Class, division, Date of Birth, Blood group,
Contact address, telephone number, driving license no. etc

Expected Output:
Display personnel information from database. The result in following format:
Name Roll No ……. Driving License No
1
2
.
.
n

Conclusion :
Hence, we have successfully implemented C++ program using concept of constructor, default
constructor, copy constructor, destructor, static member functions, friend class, this pointer,
inline code and dynamic memory allocation operators-new and delete.

_____________________________________________________________________________________________
Department of Computer Engineering Page 11 DYPCOE, Akurdi, Pune

You might also like