Вы находитесь на странице: 1из 50

IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

CONTENTS

1. Vision Mission of the Institute


2. Vision Mission of the Department
3. PEOs
4. POs
5. PSOs
6. COs
7. Content beyond Syllabus. ( if any)
8. Laboratory Regulations and Safety Rules
9. Index
10. Experiments

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Vision of the Institute

To be the fountain head of novel ideas & innovations in science & technology & persist to be
a foundation of pride for all Indians.

Mission of the Institute

 To provide value based broad Engineering, Technology and Science where education in
students is urged to develop their professional skills.

 To inculcate dedication, hard work, sincerity, integrity and ethics in building up overall
professional personality of our student and faculty.

 To inculcate a spirit of entrepreneurship and innovation in passing out students.

 To instigate sponsored research and provide consultancy services in technical, educational


and industrial areas.

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Vision of the Department

Attaining global recognition in computer science and engineering education, research and
training to meet the growing needs of the industry and society.

Mission of the Department

Provide quality undergraduate and postgraduate education, in both the theoretical and applied
foundations of computer science, and train students to effectively apply this education to solve
real-world problems, thus amplifying their potential for lifelong high-quality careers.

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Program Education Objectives (PEOs)

1. To prepare students for successful careers in software industry that meet the needs of
Indian and multinational companies.

2. To develop the skills among students to analyze real world problem & implement with
computer engineering solution and in multidisciplinary projects.

3. To provide students with solid foundation in mathematical, scientific and engineering


fundamentals to solve engineering problems and required to pursue higher studies.

4. To develop the ability to work with the core competence of computer science &
engineering i.e. software engineering, hardware structure & networking concepts so that
one can find feasible solution to real world problems.

5. To inseminate in student’s professional and ethical attitude, effective communication


skills, team work skills, multidisciplinary approach, and an ability to relate engineering
issues to broader social context.

6. To motivate students perseverance for lifelong learning and to introduce them to


professional ethics and codes of professional practice.

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Program Outcomes (POs)

PO1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering


fundamentals, and an engineering specialization to the solution of complex engineering
problems.

PO2. Problem analysis: Identify, formulate, research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.

PO3.Design/development of solutions: Design solutions for complex engineering problems and


design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.

PO4. Conduct investigations of complex problems: Use research-based knowledge and


research methods including design of experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.

PO5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex engineering
activities with an understanding of the limitations.

PO6. The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice. 44

PO7. Environment and sustainability: Understand the impact of the professional


engineering solutions in societal and environmental contexts, and demonstrate the knowledge of,
and need for sustainable development.

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

PO8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.

PO9. Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.

PO10. Communication: Communicate effectively on complex engineering activities with the


engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.

PO11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.

PO12. Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological change

Program Specific Outcomes (PSOs)

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

1. Apply Concepts in core areas of computer science -Data Structures, Database


Management System, Operating System, Computer Networks and Software Engineering
to solve technical issues.
2. Apply Software Engineering principal and practices to provide software solutions.
3. Ability to work in team and apply the knowledge acquired to develop new real life
system and able to adapt societal need of future.

Course Outcome of OOPM (CS-305)

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

CO1: State the fundamental concepts in object oriented approach. BT1


CO2: Identify and demonstrate concept of classes, objects and members of classes with their
visibility control. BT2, BT3
CO3: Demonstrate the relationship among classes with various forms of inheritance. BT3
CO4: Analyze polymorphic behavior of objects. BT4
CO5: demonstrate the concept of exception handling and multithreading. BT3

Content beyond Syllabus (if any)

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

1. Write a program to implement tic-tac-toe game using C++.


2. Write a program to implement Template using C++.

Laboratory Regulations and Safety Rules

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

1. Without Prior permission do not enter into the Laboratory.


2. While entering into the LAB students should wear their ID cards.
3. The Students should come with proper uniform.
4. Student should not use mobile phone inside the laboratory.
5. Students should sign in the LOGIN REGISTER before entering into the laboratory.
6. Students should come with observation and record note book to the laboratory.
7. Do not change any computer setting.
8. Students should maintain silence inside the laboratory.
9. After completing the laboratory exercise, make sure to SHUTDOWN the system properly.

INDEX
S.
Experiment Name Date Grade Signature
No.

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

1. WAP to illustrate the use of classes and object. (CO2)


2. WAP to illustrate different types of objects as local, global,
static and dynamic object.(CO2)
3 WAP to illustrate the concept of file handling.(CO1)
4 WAP to illustrate the use of encapsulation. (CO2)
5 WAP to illustrate static data member.(CO2)

6 WAP to illustrate the concept of constructor and destructor.


(CO2)
7 WAP to illustrate different types of inheritance.(CO3)
8 WAP to illustrate the concept of aggregation.(CO3)
9 WAP to illustrate the concept of static polymorphism.
(CO4)
10 WAP to illustrate the concept of dynamic polymorphism.
(CO4)
11 WAP to illustrate the concept of String with methods.
(CO5)
12 WAP to illustrate the concept of exception handling. (CO5)
13 WAP to illustrate the concept of Multithreading. (CO5)
Content beyond Syllabus
14 Write a program to implement tic-tac-toe game using C++.
15 Write a program to implement Template using C++.

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Experiment 1
//1. Write a Program to illustrate the use of classes and object.

#include <iostream.h>
#include<conio.h>
class Student
{ private:
int rollno;
char name[20];
float age;
public:
void getdata()
{
cout<<"\tenter the roll number:";
cin>>rollno;
cout<<"\tenter name of student:";
cin>>name;
cout<<"\tenter the age of student:";
cin>>age;
}
void showdata()
{
cout<<"\n\tRoll Number: "<<rollno;
cout<<"\n\tName of Student: "<<name;
cout<<"\n\tAgeofStudent : "<<age;
}
};
void main()
{
clrscr();
Student s1;
s1.getdata();
s1.showdata();
getch();
}

OUTPUT

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Enter the roll number :36


Enter name of student :Arbaz
Enter the age of student:18.0

Roll Number: 36
Name of Student : Arbaz
Age of student :18

Viva Questions
Q.1 what do you mean by classes?
Ans- A class describes a group of objects with similar properties, common behavior and
common relationship with each other. It is a user-defined data type which has some data
members and member functions.

Q.2 what do you mean by objects?


Ans- A object is a real world entity, which has some state and behavior objects are basic run time
entity of the programming, based on object orientation.

Q.3 how object is represented in memory?


Ans-An object is basic runtime entity in an object oriented system. Programming problems are
analyzed in terms of objects. Objects take up space in the memory and have an associated
address. These are also known as class variables.

Experiment 2

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

//2. Write a Program to illustrate different types of objects as local, global, static dynamic
object.
#include<iostream.h>
#include<conio.h>
class object
{
int value;
public:
object()
{
value=0;
}
voidaddvalue(intvaluein)
{
value+=valuein;
}
intgetvalue()
{
return value;
}
};
object obj1; //global object
void main()
{
clrscr();
object *obj=new object(); //dynamic object
cout<<"\n\tLOCAL-GLOBAL-STATIC-DYNAMIC OBJECT\n\n";
obj1.addvalue(5);
for(int i=0;i<2;i++)
{
object obj2; //local object
obj2.addvalue(10);
static object obj3; //static object created
obj3.addvalue(15);
cout<<"local object has value"<<obj2.getvalue()<<endl;
cout<<"static object has value"<<obj3.getvalue()<<endl;
cout<<"GLOBAL object has value"<<obj1.getvalue()<<endl;
//global either in or out of for always same value
}
getch();
}

OUTPUT

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Viva Questions
Q.1 Explain local object and global object?
Ans-
 External object- These type of objects are declared like a global object, The
scope of these type of objects is the entire file. The visibility mode of these type
of objects is throughout the program.

 Local object- These objects are visible throughout the local scope and has limited
lifetime.

Q.2 Explain static object and dynamic object?


Ans-
 Dynamic object- These objects are created at runtime and deleted as per as
requirement only within the required scope, these objects can be used.

 Static Object- These objects are defined locally and the scope and lifetime is
within the program only.

Experiment 3
//3. Program to illustrate the concept of file handling.

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
fstream file;
file.open("sample.txt",ios::out);

if(!file)
{
cout<<"Error in creating file!!!";
return 0;
}

cout<<"File created successfully.";

file.close();

return 0;
}

OUTPUT

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Viva Questions
Q.1 Describe how files can be opened and closed in a program?
Ans- The file operations associated with the object of one of the classes: ifstream, ofstream or
fstream. We need to create a object of corresponding class.
File can be opened by function open (). The syntax of file open is
Open (filename, mode)
Ofstreamobj;
Obj.open(“abc.txt”,ios::out);
Obj.close();
That means file abc.txt is opened in output mode. Function close () is used to close file.
Q.2 Explain the following functions:
1. Seekg() 2. Seekp()
3. Tellg() 4. Tellp()

Ans- seekg() - Sets the position of the get pointer. The get pointer determines the next location to
be read in the source associated to the stream.
Syntax: seekg (position);

seekp()- The seekp method changes the location of a stream object's file pointer for output (put
or write.) In most cases, seekp also changes the location of a stream object's file pointer for input
(get or read.)
seekp ( position );

tellg()- The tellg() function is used with input streams, and returns the current "get" position of
the pointer in the stream.
Syntax: pos_typetellg();
tellp()- Returns the absolute position of the put pointer. The put pointer determines the location
in the output sequence where the next output operation is going to take place.
Syntax: pos_typetellp();

Experiment 4
//4. Write a program to illustrate the use of encapsulation.

#include<iostream>
using namespace std;

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

class Encapsulation
{
private:
// data hidden from outside world
int x;

public:
// function to set value of
// variable x
void set(int a)
{
x =a;
}

// function to return value of


// variable x
int get()
{
return x;
}
};

// main function
int main()
{
Encapsulation obj;

obj.set(5);

cout<<obj.get();
return 0;
}

OUTPUT
5

Viva Questions
Q.1 Explain Encapsulation and its importance in programming?

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Ans-Wrapping up of data and function into a single unit is known as Encapsulation. It helps to
avoid access to private data members of our class and hence data hiding can be achieved.

Q.2 Difference between Encapsulation and Abstraction?


Ans- Encapsulation means that the internal representation of an object is generally hidden from
view outside of the object’s definition.
Abstraction is mechanisms which represent the essential features without including
implementation details.

Experiment 5
//5. Write a program to illustrate the concept of static data member.

#include <iostream>
using namespace std;

class Demo

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

{
private:
static int X;

public:
static void fun()
{
cout <<"Value of X: " << X << endl;
}
};

//defining
int Demo :: X =10;

int main()
{
Demo X;

X.fun();

return 0;
}

Output:

Viva Question:

Q.1 what is Static Member Function?

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

We can define class member’s static using static keyword. When we declare a member of a class
as static it means no matter how many objects of the class are created, there is only one copy of
the static member. A static member is shared by all objects of the class. All static data is
initialized to zero when the first object is created, if no other initialization is present.

Q.2 what is importance of static variable in function.


When a variable is declared as static, space for it gets allocated for the lifetime of the program.
Even if the function is called multiple times, space for the static variable is allocated only once
and the value of variable in the previous call gets carried through the next function call. This is
useful for implementing coroutines in C/C++ or any other application where previous state of
function needs to be stored.

Experiment 6
//6. Program to illustrate the concept of constructor and destructor.

#include<conio.h>
#include<iostream.h>
class student{

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

char name[20],add[20];
int roll;
long zip;
public:
student ( ); // Default Constructor
~student( ); //Destructor
void read( );
void display( );
};
student :: student( )
{
cout<<”This is Student Details”<<endl;
}
void student :: read( ){
cout<<”\nEnter the student Name\n”;
cin>>name;
cout<<”\nEnter the student roll no \n“;
cin>>roll;
cout<<”\nEnter the student address\n”;
cin>>add;
cout<<”\nEnter the Zipcode\n”;
cin>>zip;
}
void student :: display( ){
cout<<”Student Name :”<<name<<endl;
cout<<”Roll no is :”<<roll<<endl;
cout<<”Address is :”<<add<<endl;
cout<<”Zipcode is :”<<zip; }
student : : ~student( )
{
cout<<”Student Detail is Closed”;
}
void main( ){
student s;
clrscr( );
s.read ( );
s.display ( );
getch( );
}

OUTPUT

Enter the student Name:

Arbaz Mansuri

Enter the student roll no

105

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Enter the student address

Indore

Enter the Zipcode

453331

Student Name :Arbaz Mansuri

Roll no is :105

Address is : Indore

Zipcode is :453331

Viva Questions
Q.1 Explain the concept of constructor and destructor?
Ans- Constructor is used to initialize the variables and those variables are then used for processing. If variables are
not initialized then it contains some garbage value. Constructor has no return type and name same as class name.
Destructor is used to destroy or used to free up the memory occupied by our object. It is followed by a tilde sign
(~), as constructors it has no return type and has same name as class name.
Q.2 Explain types of constructor?
Ans.There are 3 basic type of constructor:-
 Default Constructor- A Default constructor has either no parameter, or it has parameters, all the
parameters have default values. if no user-defined constructor exists then compiler provides default
constructor implicitly.
 Parameterized Constructor- When a object is declared in parameterized constructor, the initial values
have to be passed as arguments to the constructor function.
Copy Constructor- The copy constructor is a constructor which creates an object by initializing it with an object of
the same class, Which has been created previously. The copy constructor is used to initialize one object from
another of the same type.

Experiment 7
// 7. Write a Program to illustrate different types of inheritance:
// 7.1 Single Inheritance

#include <iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle()

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

{
cout << "This is a Vehicle" << endl;
}
};

// sub class derived from two base classes


class Car: public Vehicle{

};

// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}

OUTPUT

This is a vehicle
//7.2 Hierarchical Inheritance

#include <iostream>
using namespace std;

// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

}
};

// first sub class


class Car: public Vehicle
{

};

// second sub class


class Bus: public Vehicle
{

};

// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base class
Car obj1;
Bus obj2;
return 0;
}

OUTPUT
This is a Vehicle
This is a Vehicle
//7.3 Multilevel Inheritance

#include <iostream>
using namespace std;
  
class Vehicle 
{
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
class fourWheeler: public Vehicle

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

{  public:
    fourWheeler()
    {
      cout<<"Objects with 4 wheels are vehicles"<<endl;
    }
};
class Car: public fourWheeler{
   public:
     car()
     {
       cout<<"Car has 4 Wheels"<<endl;
     }
};
  
int main()
{   
    Car obj;
    return 0;
}

OUTPUT

This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels

//7.4 Multiple Inheritance


#include <iostream>
using namespace std;
  
class Vehicle {
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
  
class FourWheeler {
  public:
    FourWheeler()

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

    {
      cout << "This is a 4 wheeler Vehicle" << endl;
    }
};
  
class Car: public Vehicle, public FourWheeler {
  
};
  
int main()
{  
    Car obj;
    return 0;
}

OUTPUT

This is a Vehicle
This is a 4 wheeler Vehicle

//7.5 Hybrid Inheritance


#include <iostream>
using namespace std;
  
class Vehicle 
{
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
  
class Fare
{
    public:

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

    Fare()
    {
        cout<<"Fare of Vehicle\n";
    }
};
  
class Car: public Vehicle
{
  
};
  
class Bus: public Vehicle, public Fare
{
      
};
  
int main()
{   
    Bus obj2;
    return 0;
}

OUTPUT

This is a Vehicle
Fare of Vehicle

Viva Questions
Q.1 what is inheritance? Explain different type of inheritance?
Ans- Inheritance is a property in which some of the properties and methods of base class can be
derived by the derived class.
TYPES OF INHERITANCE:
 Single Inheritance: In single inheritance there is one parent class. This is the most
common form of inheritance.

 Multiple inheritance: In multiple inheritance the derived from more than one base class.

 Multi-level inheritance: When a derived class is derived from a base class which itself is
a derived class then that type of inheritance is called multilevel inheritance.

 Hybrid inheritance: When two or more type of inheritances are combined together then
it forms the hybrid inheritance. It is combination of multiple and hierarchical inheritance.

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

 Hierarchical inheritance: When several base classes are derived from single base class
then it is known as hierarchicalinheritance.

Q.2 what are the advantages and disadvantages of inheritance?


Ans. Advantages :-
1. Code reusablitiy as the same base class could be used by many child classes adding their
specific properties.
2. Accessbility, while inheriting a parent class the child class's declaration specifies the
access ( public, private or protected ) .So, one could restrict the visibility of the parent
class to enhance security.
Disadvantage:- Unnecessarily adds complexity for very small programs.

Experiment 8
//8. Write a Program to illustrate the concept of Aggregation.

#include <iostream>
using namespace std;
class Address {
public:
string addressLine, city, state;
Address(string addressLine, string city, string state)
{
this->addressLine = addressLine;
this->city = city;
this->state = state;
}
};
class Employee

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

{
private:
Address* address; //Employee HAS-A Address
public:
int id;
string name;
Employee(int id, string name, Address* address)
{
this->id = id;
this->name = name;
this->address = address;
}
void display()
{
cout<<id <<" "<<name<< " "<<
address->addressLine<< " "<< address->city<< " "<<address->state<<endl;
}
};
int main(void) {
Address a1= Address("C-146, Sec-15","INDORE","MP");
Employee e1 = Employee(101,"ARBAZ",&a1);
e1.display();
return 0;

OUTPUT
101 ARBAZ C-146, Sec-15 INDORE MP

Viva Questions:
Q.1 what do you mean by term aggregation?
Ans- Aggregation is used to represent the whole to a part relationship. It normally posses the
Has-a relationship. It is denoted by-
-----------------<>
Q.2 Explain different types of Aggregation?
Ans-
 Fixed Aggregation: In this type of aggregation the number and types of the
component parts are fixed are predefined.

 Variable Aggregation: In this type of aggregation the number of parts of the


component is variable.

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

 Recursive Aggregation: The aggregation in which the object contains the


component of its own types.

Experiment 9
//9.Write a program to illustrate the concept of static polymorphism.
//9.1 Function Overloading

#include <iostream>
using namespace std;

void print(int i) {
cout << " Here is int " << i << endl;
}
void print(double f) {
cout << " Here is float " << f << endl;
}
void print(char const *c) {
cout << " Here is char* " << c << endl;
}

int main() {

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

print(10);
print(10.10);
print("ten");
return 0;
}

OUTPUT

Here is int 10
Here is float 10.1
Here is char* ten

//9.2 Operator Overloading

#include<iostream>
using namespace std;
int mul(int,int);
float mul(float,int);

int mul(int a,int b)


{
return a*b;
}
float mul(double x, int y)
{
return x*y;
}
int main()
{
int r1 = mul(6,7);

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

float r2 = mul(0.2,3);
std::cout << "r1 is : " <<r1<< std::endl;
std::cout <<"r2 is : " <<r2<< std::endl;
return 0;
}

OUTPUT

r1 is : 42
r2 is : 0.6

Viva Questions

Q.1 what do you mean by polymorphism?


Ans- The word “poly” originated from Greek word meaning many and “morphism” from a
Greek word meaning form, and thus polymorphism means many forms. It is an important feature
of OOP. It is an ability to take more than one form.

Q.2 Explain types of static polymorphism?


Ans. It is also known as compile time polymorphism. It can be implemented by two ways as
follows:

(a) Function overloading: Overloading means to use of the same thing for different purposes,
C++ also permits overloading of functions by the use of same functions name to create functions
that perform a variety of task.

(b) Operator overloading: Operator overloading provide multiple meanings to the set of
predefined operators of the compiler. So as to use it for multiple tasks.

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Experiment 10
//10. Program to illustrate the concept of dynamic polymorphism
#include <iostream>
using namespace std;

class Shape {
protected:
int width, height;

public:
Shape( int a = 0, int b = 0){
width = a;
height = b;
}
int area() {
cout << "Parent class area :" <<endl;
return 0;
}
};
class Rectangle: public Shape {
public:

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Rectangle( int a = 0, int b = 0):Shape(a, b) { }

int area () {
cout << "Rectangle class area :" <<endl;
return (width * height);
}
};

class Triangle: public Shape {


public:
Triangle( int a = 0, int b = 0):Shape(a, b) { }

int area () {
cout << "Triangle class area :" <<endl;
return (width * height / 2);
}
};

// Main function for the program


int main() {
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);

// store the address of Rectangle


shape = &rec;

// call rectangle area.


shape->area();

// store the address of Triangle


shape = &tri;

// call triangle area.


shape->area();

return 0;
}

OUTPUT
Parent class area :
Parent class area :

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Viva Questions
Q.1 what do you mean by Dynamic polymorphism?
Ans- Dynamic polymorphism is also known as run-time polymorphism. Dynamic (run
time) polymorphism is the polymorphism existed at run-time. Here, Java compiler does not
understand which method is called at compilation time. Only JVM decides which method is
called at run-time. Method overloading and method overriding using instance methods are the
examples for dynamic polymorphism.
Q.2 what are the difference between Static and Dynamic polymorphism?
Ans-
STATIC POLYMORPHISM DYNAMIC POLYMORPHISM

In Compile time Polymorphism, call is In Run time Polymorphism, call


resolved by the compiler. is not resolved by the compiler.

It is also known as Static binding, Early It is also known as Dynamic binding, Late
binding and overloading as well. binding and overriding as well.

Overloading is compile time polymorphism Overriding is run time polymorphism having


where more than one methods share the same same method with same parameters or
name with different parameters or signature signature, but associated in a class & its
and different return type. subclass.

It is achieved by function overloading It is achieved by virtual


and operator overloading. functions and pointers

It provides fast execution because known It provides slow execution as compare to early


early at compile time. binding because it is known at runtime.

Compile time polymorphism is less flexible as Run time polymorphism is more flexible as
all things execute at compile time. all things execute at run time.

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Experiment 11

//11. Program to illustrate the concept of String with methods.

class Testimmutablestring1
{
public static void main(String args[]){
String s1="Arbaz";
String s2="ARBAZ";
//System.out.println(s1.equals(s2));
String s="Arbaz";
s=s.concat(" Mansuri");
System.out.println(s.substring(6));
System.out.println(s1.equalsIgnoreCase(s2));//true
System.out.println(s1==s2);
System.out.println(s1.compareTo(s2));
System.out.println(s);
}
}

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

OUTPUT
Mansuri
true
false
32
Arbaz Mansuri

Viva Questions
Q. 1 Why is String final in Java?
There are a couple of reasons for this e.g. String pool, Caching, Performance but Security is
probably the most important reason. Given String is used in sensitive places like specifying the
host and port details, locking it for modification prevent from may Security related risks.

Q.2 Is String a keyword in java?


No. String is not a keyword in java. String is a final class in java.lang package which is used to
represent the set of characters in java.

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Experiment 12

//12. Program to illustrate the concept of Exception handling.


class Example
{
public static void main(String args[]) {
int d = 0;
int n = 20;
try
{
int fraction = n / d;
System.out.println("This line will not be Executed");
} catch (ArithmeticException e)
{
System.out.println("In the catch Block due to Exception = " + e);
}
System.out.println("End Of Main");
}
}

Output-
In the catch Block due to Exception = java.lang.ArithmeticException: / by zero

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

End Of Main

Viva Questions
Q.1 what do you mean by an Exception?
Ans- An exception is a problem that arises during the execution of a program. A C++ exception
is a response to an exceptional circumstance that arises while a program is running, such as an
attempt to divide by zero.
Exceptions provide a way to transfer control from one part of a program to another. C++
exception handling is built upon three keywords: try, catch, and throw.
Q.2 Explain blocks contained in an Exception handler?
Ans-
 Try block:
Statements which will raise errors at runtime are written in this block.
Syntax-
try(Expression);

 Catch block: Runtime errors are caught by the catch block; It catches the Exception
thrown by the throw statement in try block.
Syntax-
catch()
{
-------------
}

 Throw block: If an Exception occurs in try block then it is thrown by this statement.
Syntax:
Throw(object)
{
------------;

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

------------;
}

Experiment 13
//13. Program to illustrate the concept of multithreading.

13.1- Java Thread Example by extending Thread class

class Multi extends Thread{  
public void run(){  
System.out.println("thread is running...");  
}  
public static void main(String args[]){  
Multi t1=new Multi();  
t1.start();  
 }  
}  
Output
thread is running...

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

13.2- Java Thread Example by implementing Runnable interface

class Multi3 implements Runnable{  
public void run(){  
System.out.println("thread is running...");  
}  
  
public static void main(String args[]){  
Multi3 m1=new Multi3();  
Thread t1 =new Thread(m1);  
t1.start();  
 }  
}  

Out put

thread is running...

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Viva Questions
Q.1 what is Thread.
The thread is a program execution that uses process resources for accomplishing the task. All
threads within a single program are logically contained within a process. The kernel allocates a
stack and a thread control block (TCB) to each thread. The operating system saves only the stack
pointer and CPU state at the time of switching between the threads of the same process.
Q.2 what is Multithreading and Multitasking.
Comparison Chart
Basis for
Multitasking Multithreading
Comparison

Multitasking let CPU to execute Multithreading let CPU to execute multiple


Basic
multiple tasks at the same time. threads of a process simultaneously.

In multitasking CPU switches In multithreading CPU switches between the


Switching
between programs frequently. threads frequently.

In multitasking system has to In multithreading system has to allocate


Memory and allocate separate memory and memory to a process, multiple threads of that
Resource resources to each program that process shares the same memory and
CPU is executing. resources allocated to the process.

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

//Content Beyond Syllabus


Experiment 14
//14.Write a program to implement tic-tac-toe game using C++.

#include <iostream.h>
#include <conio.h>
char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
intcheckwin();
void board();
void main()
{
int player = 1,i,choice;
char mark;
clrscr();
do
{
board();
player=(player%2)?1:2;
cout<< "Player " << player << ", enter a number: ";
cin>> choice;
mark=(player == 1) ? 'X' : 'O';
if (choice == 1 && square[1] == '1')
square[1] = mark;
else if (choice == 2 && square[2] == '2')
square[2] = mark;
else if (choice == 3 && square[3] == '3')
square[3] = mark;
else if (choice == 4 && square[4] == '4')
square[4] = mark;
else if (choice == 5 && square[5] == '5')
square[5] = mark;

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

else if (choice == 6 && square[6] == '6')


square[6] = mark;
else if (choice == 7 && square[7] == '7')
square[7] = mark;
else if (choice == 8 && square[8] == '8')
square[8] = mark;
else if (choice == 9 && square[9] == '9')
square[9] = mark;
else
{
cout<<"Invalid move ";
player--;
getch();
}
i=checkwin();
player++;
}
while(i==-1);
board();
if(i==1)
cout<<"==>\aPlayer "<<--player<<" win ";
else
cout<<"==>\aGame draw";
getch();

}
intcheckwin()
{
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[4] == square[5] && square[5] == square[6])
return 1;
else if (square[7] == square[8] && square[8] == square[9])
return 1;
else if (square[1] == square[4] && square[4] == square[7])
return 1;
else if (square[2] == square[5] && square[5] == square[8])
return 1;
else if (square[3] == square[6] && square[6] == square[9])
return 1;
else if (square[1] == square[5] && square[5] == square[9])
return 1;
else if (square[3] == square[5] && square[5] == square[7])
return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3' &&
square[4] != '4' && square[5] != '5' && square[6] != '6' &&

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

square[7] != '7' && square[8] != '8' && square[9] != '9')


return 0;
else
return -1;
}
void board()
{
clrscr();
cout<< "\n\n\tTicTac Toe\n\n";
cout<< "Player 1 (X) - Player 2 (O)" <<endl<<endl;
cout<<endl;
cout<< " | | " <<endl;
cout<< " " << square[1] << " | " << square[2] << " | " << square[3] <<endl;
cout<< "_____|_____|_____" <<endl;
cout<< " | | " <<endl;
cout<< " " << square[4] << " | " << square[5] << " | " << square[6] <<endl;
cout<< "_____|_____|_____" <<endl;
cout<< " | | " <<endl;
cout<< " " << square[7] << " | " << square[8] << " | " << square[9] <<endl;
cout<< " | | " <<endl<<endl;

OUTPUT:

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Viva Question:
Q.1 How to find the winner of a tic-tac-toe game of any size?
Ans- To start, have an array of numbers for each row, column, and diagonal for each player. On
each move, increment the elements corresponding to the player for the row, column, and
diagonal (the move may not necessarily be on a diagonal) influenced by that move. If the count
for that player is equal to the dimension of the board, that player wins.
Q.2 What does checkwin() function do?
Ans- It helps to find which checked block so that no two blocks can be shared by different player
and they put their symbols one by one and finally display the winner.

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

Experiment 15
//15. Write a program to implement Template using C++.
15.1- Function Template
#include <iostream>
using namespace std;
template<class T> T add(T &a,T &b)
{
T result = a+b;
return result;

}
int main()
{
int i =2;

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

int j =3;
float m = 2.3;
float n = 1.2;
cout<<"Addition of i and j is :"<<add(i,j);
cout<<'\n';
cout<<"Addition of m and n is :"<<add(m,n);
return 0;
}

Output
Addition of i and j is :5                                                  
                                                    
Addition of m and n is :3.5 

15.2- class template contains two generic data types


#include <iostream>
using namespace std;
template<class T1, class T2>
class A
{
T1 a;
T2 b;
public:
A(T1 x,T2 y)
{
a = x;
b = y;
}
void display()

3rd SEM OOPM (CS305) 2018-19


IPS ACADEMY, INSTITUTE OF ENGINEERING AND SCIENCE- INDORE

{
std::cout << "Values of a and b are : " << a<<" ,"<<b<<std::endl;
}
};

int main()
{
A<int,float> d(5,6.5);
d.display();
return 0;
}
Output-

Values of a and b are : 5 ,6.5                                             
                                                    
                                  

Viva Questions:
Q.1 What do you mean by templates?

Templates are a feature of the C++ programming language that allows functions and classes to
operate with generic types. This allows a function or class to work on many different data types
without being rewritten for each one.

Q2. How many types of Templete used in c++?


Ans- There are two kinds of templates: function templates and class templates.

3rd SEM OOPM (CS305) 2018-19

Вам также может понравиться