0% found this document useful (0 votes)
34 views61 pages

OOP Lab

The document discusses programming concepts in C++ and Java. It provides examples of basic programs in each language that calculate the greatest common divisor (GCD), check if a number is prime, print multiplication tables, and calculate the roots of a quadratic equation. It then shows how to write these same programs using user-defined functions in C++ and Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views61 pages

OOP Lab

The document discusses programming concepts in C++ and Java. It provides examples of basic programs in each language that calculate the greatest common divisor (GCD), check if a number is prime, print multiplication tables, and calculate the roots of a quadratic equation. It then shows how to write these same programs using user-defined functions in C++ and Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 61

20331A0517 Object Oriented Programming Lab CSE-2A

Week – 1
Aim: To learn Basic programs in C++ and Java.
1. Write a CPP program for accepting user inputs and display it.

Description:
In C++ to access the output stream, we need to use the ‘std’ namespace. As it is by
default private, we need to access it through the scope resolution operator.

Program:
#include<iostream>
int main() {
std::cout << "Hello world!" << std::endl;
}
Output:
Hello world!

2. Write a CPP program for GCD, PRIME NUMBER, Multiplication Table, roots of
quadratic equation

Description:
In C++ programming “using namespace std” header allows us to directly access
the std namespace methods. There are 4 method in std namespace they are: cout,
cin, cerr and clog.

a) GCD.cpp:
#include <iostream>
using namespace std;
int main() {
int n1, n2, GCD;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
if ( n2 > n1) {
int temp = n2;
n2 = n1;
n1 = temp;
}
for(int i = 1; i <= n2; i++) {

MVGR College of engineering Autonomous 1


20331A0517 Object Oriented Programming Lab CSE-2A

if (n1 % i == 0 && n2 % i ==0) {


GCD = i;
}
}
cout << "The GCD of " << n1 << " and " << n2 << " is " << GCD;
return 0;
}
Output:
Enter the two numbers : 14 49
The GCD of 14 and 49 is 7

b) PRIME.cpp:
#include<iostream>
using namespace std;
int main() {
int n,i,count = 0;
cout << "Enter the number : ";
cin >> n;
for(i=2; i<n; i++) {
if (n % i == 0)
count ++;
}
cout << n <<" is a ";
if(count != 0)
cout << "COMPOSITE number";
else
cout << "PRIME number";
return 0;
}
Output:
Enter the number : 7
7 is a PRIME number

c) MULTIPLICATION.cpp:
#include<iostream>
using namespace std;
int main(){
int a, i, n;
cout << "Enter table format : ";
cin >> a;

MVGR College of engineering Autonomous 2


20331A0517 Object Oriented Programming Lab CSE-2A

cout << "Enter end of table : ";


cin >> n;
for(i = 0; i <= n; i++){
cout << a << " X " << i << " = " << a*i << endl;
}
}
Output:
Enter table format : 2
Enter end of table : 8
2X0=0
2X1=2
2X2=4
2X3=6
2X4=8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16

d) ROOTS.cpp
#include <iostream>
#include <cmath>
using namespace std;

int main() {
float a, b, c, x1, x2, d, r, i;
cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
d = b*b - 4*a*c;
if (d > 0) {
x1 = (-b + sqrt(d)) / (2*a);
x2 = (-b - sqrt(d)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (d == 0) {
cout << "Roots are real and same." << endl;
x1 = -b/(2*a);
cout << "x1 = x2 =" << x1 << endl;

MVGR College of engineering Autonomous 3


20331A0517 Object Oriented Programming Lab CSE-2A

}
else {
r = -b/(2*a);
i = sqrt(-d)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << r << " + (" << i << ") i" << endl;
cout << "x2 = " << r << " - (" << i << ") i" << endl;
}
return 0;
}
Output:
Enter coefficients a, b and c: 1 2 3
Roots are complex and different.
x1 = -1 + (1.41421) i
x2 = -1 - (1.41421) i

3. Write a JAVA program for accepting user inputs and displaying them.

Description:
In java, we need to write them in a class and those methods or attributes can be
accessed through the class which is having the main class. Also the name of the
program should be same as the main class name.

Program:
class Hello {
public static void main(String args[]) {
System.out.println("Hello Java!");
}
}
Output:
Hello Java!

4. Write a JAVA program for GCD, PRIME NUMBER, Multiplication Table

Description:
In order to give inputs in java, we use some special in-built classes that allow us to
reduce the code and also reuse them. All these classes are under the Java class,
we need to access them through “.” Operator.

a) GCD.java:

MVGR College of engineering Autonomous 4


20331A0517 Object Oriented Programming Lab CSE-2A

import java.util.Scanner;

public class GCD {


public static void main(String[] args) {
int n1, n2, temp, GCD=1;
Scanner a = new Scanner(System.in);
System.out.println("Enter the n1 and n2 values : ");
n1 = a.nextInt();
n2 = a.nextInt();
for(int i=1;i<=n1 && i<=n2;i++) {
if( n1%i == 0 && n2%i == 0 )
GCD = i;
}
System.out.printf("GCD of %d and %d is %d",n1 ,n2 ,GCD);
}
}
Output:
Enter the n1 and n2 values :
12
56
GCD of 12 and 56 is 4

b) PRIME.java:
import java.util.Scanner;

public class Prime {


public static void main(String args[]) {
int i,m=0,flag=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer : ");
int n = sc.nextInt();
if(n==0||n==1) {
System.out.println(n+" is not prime number");
}
else {
for(i=2;i<n;i++){
if(n%i==0) {
System.out.println(n+" is COMPOSITE number");
flag=1;
break;

MVGR College of engineering Autonomous 5


20331A0517 Object Oriented Programming Lab CSE-2A

}
}
if(flag==0) {
System.out.println(n+" is PRIME number");
}
}
}
}
Output:
Enter an integer : 43
43 is prime number

c) MulTable.java
import java.util.Scanner;

public class MulTable {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter table format : ");
int n = sc.nextInt();
System.out.print("Enter end of table : ");
int m = sc.nextInt();
for(int i = 1; i <= m; ++i) {
System.out.printf("%d * %d = %d \n", n, i, n * i);
}
}
}
Output:
Enter table format : 4
Enter end of table : 9
4X1=4
4X2=8
4 X 3 = 12
4 X 4 = 16
4 X 5 = 20
4 X 6 = 24
4 X 7 = 28
4 X 8 = 32
4 X 9 = 36

MVGR College of engineering Autonomous 6


20331A0517 Object Oriented Programming Lab CSE-2A

5. Write the usage of the User-defined functions in CPP and JAVA for GCD, Prime
numbers, roots of quadratic equation

Description:
Similar to C programming in C++ also we import the header files in the same
fashion but the header files in C++ are different from the C language.

a) GCDfun.cpp:
#include <iostream>
using namespace std;
int GCD(int a,int b) {
if ( b > a) {
int temp = b;
b = a;
a = temp;
}
if (b == 0)
return a;
else
return GCD(b, a % b);
}
int main() {
int n1, n2, gcd;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
gcd = GCD(n1,n2);
cout << "The GCD of " << n1 << " and " << n2 << " is " << gcd;
return 0;
}
Output:
Enter two numbers: 12 132
The GCD of 12 and 132 is 12

b) PrimeFun.cpp:
#include<iostream>
using namespace std;
void Prime(int n) {
int count = 0,i;
for(i=2; i<n; i++) {
if (n % i == 0)

MVGR College of engineering Autonomous 7


20331A0517 Object Oriented Programming Lab CSE-2A

count ++;
}
if(count != 0)
printf("%d is a COMPOSITE number\n",n);
else
printf("%d is a PRIME number\n",n);
}
int main() {
int n;
cout << "Enter the number : ";
cin >> n;
Prime(n);
}
Output:
Enter the number : 17
17 is a PRIME number

c) RootsFun.cpp:
#include <iostream>
#include <cmath>
using namespace std;

void Roots(int a,int b,int c) {


int d;
double r,i,x1,x2;
d = b*b - 4*a*c;
if (d > 0) {
x1 = (-b + sqrt(d)) / (2*a);
x2 = (-b - sqrt(d)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (d == 0) {
cout << "Roots are real and same." << endl;
x1 = -b/(2*a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
r = -b/(2*a);

MVGR College of engineering Autonomous 8


20331A0517 Object Oriented Programming Lab CSE-2A

i = sqrt(-d)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << r << " + (" << i << ") i" << endl;
cout << "x2 = " << r << " - (" << i << ") i" << endl;
}
}
int main() {
float a, b, c, x1, x2, d, r, i;
cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
Roots(a,b,c);
return 0;
}
Output:
Enter coefficients a, b and c: 1 3 5
Roots are complex and different.
x1 = -1 + (1.65831) i
x2 = -1 - (1.65831) i

d) GCDfun.java:
import java.util.Scanner;

public class GCDfun {


public static void main(String[] args) {
Scanner a = new Scanner(System.in);
System.out.println("Enter the n1 and n2 values : ");
int n1 = a.nextInt();
int n2 = a.nextInt();
System.out.printf("GCD of %d and %d is %d\
n",n1 ,n2 ,findGCD(n1,n2));
}
static int findGCD(int a, int b) {
if (b == 0)
return a;
return findGCD(b, a % b);
}
}

Output:

MVGR College of engineering Autonomous 9


20331A0517 Object Oriented Programming Lab CSE-2A

Enter the n1 and n2 values :


13
26
GCD of 13 and 26 is 13

e) PrimeFun.java:
import java.util.Scanner;

public class Prime {


void prime(int n) {
int i,m=0,flag=0;
if(n<=1) {
System.out.println(n+" is not prime number");
}
else {
for(i=2;i<n;i++){
if(n%i==0) {
System.out.println(n+" is COMPOSITE number");
flag=1;
break;
}
}
if(flag==0) {
System.out.println(n+" is PRIME number");
}
}
}
public static void main(String args[]) {
Prime p = new Prime();
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer : ");
int n = sc.nextInt();
p.prime(n);
}
}
Output:
Enter an integer : 5
5 is PRIME number

MVGR College of engineering Autonomous 10


20331A0517 Object Oriented Programming Lab CSE-2A

6. Write the Operations of Arrays in CPP and JAVA

Description:
In C++ we declare the arrays in the same fashion but in Java it is completely
different from C and C++. It is declared as: data_type[] array_name;.
a) ArraySum.cpp:

#include <iostream>
using namespace std;
int main() {
int n, s = 0, I = 0;
cout << “Enter the size of the array : “;
cin >> n;
int arr[n];
cout << “Enter the elements in the array : “;
while(I < n) {
cin >> arr[i];
s += arr[i];
i++;
}
i=0;
cout << “The sum of “;
while(I < n) {
printf(“%d “,arr[i]);
i++;
}
cout << “ is : “ << s;
return 0;
}
Output:
Enter the size of the array : 3
Enter the elements in the array : 1 2 3
The sum of 1 2 3 is : 6

b) Arrays.java:
import java.util.Scanner;

MVGR College of engineering Autonomous 11


20331A0517 Object Oriented Programming Lab CSE-2A

public class Arrays {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] ele;
int n,v,s = 0;
I(“Enter the No. of elements : “);
n = input.nextInt();
ele = new int[n];
I(“Enter the “+n+” elements values : “);
for(int I = 0;I < n;i++) {
v = input.nextInt();
ele[i] = v;
}
for(int a : ele) {
s += a;
}
System.out.println(“Sum of “+n+” elements : “+s);
}
}
Output:
Enter the No. of elements : 5
Enter the 5 elements values : 1 2 3 4 5
Sum of 5 elements : 15

Conclusion:
In this week I had learned about:
 How to write a Basic C++ and Java program and compile them on a software.
 How to give inputs from user and display it in C++ and Java.
 How to perform different mathematical operations using C++ and Java.
 How to perform function declarations and function calls in C++ and Java.
 How to declare, initiate and access elements of array using C++ and Java.

MVGR College of engineering Autonomous 12


20331A0517 Object Oriented Programming Lab CSE-2A

Week – 2

Aim: To know about Classes, objects, and Constructors.

1. Write a CPP program for creating a class, object, and initializing class member
variables, including scope resolution.

Description:
In C++ we can declare classes, which differs us from C programming. In order to
access the methods, attributes, and constructors we need to create objects using
the class name while declaring.

Class.cpp:
#include <iostream>
using namespace std;
class Name {
public:
string name;
void printname() {
cout << "Name is : " << name;
}
};
int main() {
Name obj;
obj.name = "Ravi";
obj.printname();
return 0;
}
Output:
Name is : Ravi

2. Write a JAVA program for creating a class, object, and initializing class member
variables.

Description:
While creating objects for the base class, in C++ by default the entities inside the
class are private whereas in java it is public. So, it is necessary to initiate an access
modifier to change the class state.

MVGR College of engineering Autonomous 13


20331A0517 Object Oriented Programming Lab CSE-2A

Class.java:
public class Class {
String Name;
String Course;
int Age;
public Class(String name, String course,int age) {
Name = name;
Course = course;
Age = age;
}
public String getName() {
return Name;
}
public String getCourse() {
return Course;
}
public int getAge() {
return Age;
}
public static void main(String[] args) {
Class s1 = new Class("Ravi","CSE",54);
System.out.println(s1.getName());
System.out.println(s1.getCourse());
System.out.println(s1.getAge());
}
}
Output:
Ravi
CSE
54

3. Write the program using the constructor in CPP and JAVA.

Description:
Constructors are used to create/allocate memory for the object in a program.
By default, these will be invoked while declaring the objects for a specific class.
These can be user-defined also.

a) Constructor.cpp:
#include <iostream>

MVGR College of engineering Autonomous 14


20331A0517 Object Oriented Programming Lab CSE-2A

using namespace std;


class Employee {
public:
Employee() {
cout<<"By Default Constructor Invoked"<<endl;
}
};
int main() {
Employee e1;
Employee e2;
return 0;
}
Output:
By Default Constructor Invoked
By Default Constructor Invoked

b) Constructor.java:
class Box {
double width;
double height;
double depth;
Box() {
System.out.println("Constructing Box");
width = 100;
height = 100;
depth = 100;
}
double volume() {
return width * height * depth;
}
}
class BoxDemo {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
vol = mybox1.volume();
System.out.println("Volume of the first box is " + vol);
vol = mybox2.volume();
System.out.println("Volume of the second box is " + vol);

MVGR College of engineering Autonomous 15


20331A0517 Object Oriented Programming Lab CSE-2A

}
}

Output:
Constructing Box
Constructing Box
Volume of the first box is 1000000.0
Volume of the second box is 100000.0

4. Write a program using a different type of constructor calls in CPP.

Description:
While invoking the constructor we can pass arguments in different ways i.e.,
arguments of different datatypes, number of arguments, and sequence.

Constructor2.cpp:
#include <iostream>
using namespace std;
class Employee {
public:
int id;
string name;
float salary;
Employee(int i, string n, float s) {
id = i;
name = n;
salary = s;
}
void display() {
cout << id << " " << name <<" "<< salary <<endl;
}
};

int main() {
Employee e1 = Employee(25, "Ravi", 80000);
Employee e2 = Employee(50, "Kiran", 50000);
e1.display();
e2.display();
return 0;
}

MVGR College of engineering Autonomous 16


20331A0517 Object Oriented Programming Lab CSE-2A

Output:
25 Ravi 80000
50 Kiran 50000

5. Write a program using the Copy constructor, and destructor in CPP

Description:
In C++ the constructors can be copied in 2 ways, one is a deep copy another one is
a shallow copy. When the program terminates the destructor will be invoked
which means the destructor will de-allocate the memory for the objects.

a) CopyConstr.cpp:
#include <iostream>
using namespace std;
class A {
public:
int x;
A(int a) {
x = a;
}
A(A &i) {
x = i.x;
}
};

int main() {
A a1(20);
A a2(a1);
cout << "Variable in object a1 : " << a1.x << endl;
cout << "Variable in copied object a2 : " << a2.x;
return 0;
}
Output:
Variable in object a1 : 20
Variable in copied object a2 : 20

b) Destuctor.cpp:
#include <iostream>
using namespace std;

MVGR College of engineering Autonomous 17


20331A0517 Object Oriented Programming Lab CSE-2A

class Employee {
public:
Employee() {
cout<<"Constructor Invoked"<<endl;
}
~Employee() {
cout<<"Destructor Invoked"<<endl;
}
};
int main() {
Employee e1;
Employee e2;
return 0;
}
Output:
Constructor Invoked
Constructor Invoked
Destructor Invoked
Destructor Invoked

Conclusion:
In this week I had learned about:
 How to create classes, and objects and initialize the objects under a class in C++
and java.
 How to give various methods in a class and access them through objects in C++
and java.
 Understanding the purpose of constructor and destructor in C++ and java.
 How to use the constructor with and without parameters in C++ and java.
 How to copy the constructors of one object to another object in C++.

MVGR College of engineering Autonomous 18


20331A0517 Object Oriented Programming Lab CSE-2A

Week – 3
Aim: To get familiar with overloading, inline, static functions, and
command-line arguments in C++ and java.

1. Write a program showing Method Overloading in CPP, JAVA

Description:
In C++ and java, we create the same method with different parameters such that
the method is accessed based on the parameters.

a) MethodOL.cpp:
#include<iostream>
using namespace std;
class PrintData {
public:
void print(int a) {
cout << a << " is a Integer.\n";
}
void print(string a) {
cout << a << " is a String.\n";
}
void print(double a) {
cout << a << " is a Float.\n";
}
};
int main() {
PrintData d;
d.print(5);
d.print("ravi");
d.print(5.54);
return 0;
}
Output:
5 is a Integer.
ravi is a String.
5.54 is a Float.

b) MethodOL.java:
public class MethodOL {

MVGR College of engineering Autonomous 19


20331A0517 Object Oriented Programming Lab CSE-2A

public void disp(String a) {


System.out.println(a+" is a String");
}
public void disp(char a) {
System.out.println(a+" is a Character");
}
public void disp(int a) {
System.out.println(a+" is an Integer");
}
public void disp(double a) {
System.out.println(a+" is a Float");
}
}

class Display {
public static void main(String args[]) {
MethodOL obj = new MethodOL();
obj.disp("Ravi");
obj.disp(5);
obj.disp(5.54);
obj.disp('R');
}
}
Output:
Ravi is a String
5 is an Integer
5.54 is a Float
R is a Character

2. Write a program using Static keywords, static methods static blocks usage.

Description:
In C++ and Java, we create the variable method and blocks with a static keyword
such that only one copy of a member is created for the entire class shared by all
objects of that class.

a) StaticKeyword.cpp:
#include <iostream>
#include <string>
using namespace std;

MVGR College of engineering Autonomous 20


20331A0517 Object Oriented Programming Lab CSE-2A

void Static ()
{
static int a = 0;
cout << a << " ";
a++;
}
int main()
{
for (int i=0; i<5; i++)
Static();
return 0;
}
Output:
01234

b) StaticMethod.cpp:
#include<iostream>
using namespace std;
class Static
{
  public:
     static void print()
{
         cout<<"Hello CSE-A";
     }
};
int main()
{
     Static::print ();
}
Output:
Hello CSE-A

c) StaticBlock.cpp:
class Static{
    static int a;
    static
    {
        a= 777;
        System.out.println("static block is invoked");

MVGR College of engineering Autonomous 21


20331A0517 Object Oriented Programming Lab CSE-2A

    }
}
class StaticBlock{
    public static void main(String args[])
    {
        System.out.println(Static.a);
    }
}
Output:
777
static block is invoked

3. Write a program using Inline functions and friend function in CPP

Description:
In C++ we create a method or constructor using the keyword inline such that the
whole function is substituted at the inline function call. We create friend keyword
to access private or protected items of the class by friend class.

a) Inline.cpp:
#include <iostream>
using namespace std;
inline int factorial(int s)
{
If(s!=1)
    return s*factorial(s-1);
else
return 1;
}
int main()
{
    cout << "The 6! is: " << factorial(6) << "\n";
    return 0;
}
Output:
The 6! Is: 720

b) Friend.cpp:
#include <iostream>

MVGR College of engineering Autonomous 22


20331A0517 Object Oriented Programming Lab CSE-2A

using namespace std;


class A {
private:
    int a;
public:
    A() { a = 0; }
    friend class B;
};
 
class B {
private:
    int b;
 
public:
    void Value(A& x)
    {
        cout << "a=" << x.a;
    }
};
 
int main()
{
    A x;
    B y;
    y.Value(x);
    return 0;
}
Output:
a=0

4. Write the program showing Operator overloading, and Assignment Operator


overloading using CPP.

Description:
In C++ and Java, we create a method such that it has a special keyword operator
and consists of an operator symbol between one or more objects.

a) OperatorOverloading.cpp:
#include<iostream>

MVGR College of engineering Autonomous 23


20331A0517 Object Oriented Programming Lab CSE-2A

using namespace std;


class Complex {
private:
    int rp, imp;
public:
    Complex(int r = 0, int i = 0) {rp= r;   imp = i;}
    Complex operator + (Complex const &obj) {
         Complex res;
         res. rp = rp + obj. rp;
         res. imp = imp + obj. imp;
         return res;
    }
    void print() { cout << rp << " + i" << imp << '\n'; }
};
 
int main()
{
    Complex c1(1, 2), c2(5, 4);
    Complex c3 = c1 + c2;
    c3.print();
}
Output:
6 + i6

b) AssignmentOverloading.cpp:
#include <iostream>
using namespace std;
class Distance {
private:
int feet;
int inches;
public:
Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}

MVGR College of engineering Autonomous 24


20331A0517 Object Oriented Programming Lab CSE-2A

void operator = (const Distance &D ) {


feet = D.feet;
inches = D.inches;
}

// method to display distance


void displayDistance() {
cout << "F: " << feet << " I:" << inches << endl;
}
};

int main() {
Distance D1(11, 10), D2(5, 11);
cout << "First Distance : ";
D1.displayDistance();
cout << "Second Distance :";
D2.displayDistance();
D1 = D2;
cout << "First Distance :";
D1.displayDistance();
return 0;
}
Output:
First Distance : F: 11 I:10
Second Distance :F: 5 I:11
First Distance :F: 5 I:11

5. Write the CPP program using Command line arguments, Manipulators, string
Math header files

Description:
In C++ we create programs with header files such as string, cmath, and different
manipulators to manipulate the output and access different methods in the
header file.
We create a program that handles command-line arguments using the main
function.

a) Command.cpp:
#include<iostream>

MVGR College of engineering Autonomous 25


20331A0517 Object Oriented Programming Lab CSE-2A

int main (int argc,char *argv[]){


if (argc == 2) {
cout<<"The argument supplied is "<<argv[1]<< "\n";
}
else if (argc > 2) {
cout<<"To many arguments supplied.\n";
}
else {
cout<<"One argument expected.\n";
}
}
Output:
1. $ ./main a
The argument supplied is a
2. $ ./main a b
To many arguments supplied.
3. $./main
One argument expected.

b) Manipulator.cpp:
#include <iostream>
#include <iomanip>
#include <istream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
double A = 100;
double B = 2001.5251;
     double C = 201455.2646;
     cout << hex << left << showbase << nouppercase;
      cout << (long long)A << endl;
     cout << setbase(10) << right << setw(15) << setfill('_') << showposfixed <<
setprecision(2);
      cout << B << endl;
     cout << scientific << uppercase<< noshowpos << setprecision(9);
      cout << C << endl;

    istringstream str("           Programmer");

MVGR College of engineering Autonomous 26


20331A0517 Object Oriented Programming Lab CSE-2A

    string line;
    getline(str >> std::ws, line);
    cout << line << endl;
    cout << "only a test" << flush;
    cout << "\n";
    cout << "b" << ends;
    cout << "c" << endl;
    return 0;
}
Output:
0x64
_______+2001.53
2.014552646E+05
Programmer
Only a test
abc

c) String.cpp:
#include<string>
int main(){
string a;
string b;
cin>>a;
cin>>b;
string c = a + b;
cout<<c;
string d = a.append(b);
cout << d;
cout << "The length of d : " << d.size();
cout << "First letter of d :" << d[0];
return 0;
}
Output:
Ravi kiran varma
Ravi kiran varma
Ravi kiran varma
The length of d : 15
First letter of d : R

d) Math.cpp:

MVGR College of engineering Autonomous 27


20331A0517 Object Oriented Programming Lab CSE-2A

#include <cmath>
#include <iostream>
int main(){
cout << sqrt(64)<<endl;
cout << round(2.6)<<endl;
cout << log(10)<<endl;
return 0;
}
Output:
8
3
0.693147

CONCLUSION:
In this week I had learned about:
 How to use method overloading in C++ and java.
 Usage of static keywords, static methods, and static blocks.
 How to make inline functions using C++.
 How to use operator overloading and assignment overloading in C++.
 Usage of command-line arguments, manipulators, string and math header files.

MVGR College of engineering Autonomous 28


20331A0517 Object Oriented Programming Lab CSE-2A

WEEK 4
Aim: To study different types of inheritance and polymorphism.

1) Write a program using Inheritance & types of Inheritance in CPP, JAVA.

a) Description:
In single inheritance, a class is allowed to inherit from only one class. i.e. one
subclass is inherited by one base class only, we need to access it through the
scope resolution operator.

SingleInherit.cpp:
#include<iostream>
using namespace std;
 class Vehicle {
  public:
    Vehicle()
    {
      cout << "This is a Vehicle\n";
    }
};
class Car : public Vehicle {
};
int main()
{        
  Car obj;
    return 0;
}
Output:
This is a Vehicle

b) Description:
Multiple Inheritance is a feature of C++ where a class can inherit from more
than one classes. i.e one sub class is inherited from more than one base
classes, we need to access it through the scope resolution operator.

MVGR College of engineering Autonomous 29


20331A0517 Object Oriented Programming Lab CSE-2A

MultipleInherit.cpp:
#include<iostream>
using namespace std;
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle\n";
}
};
class Car : public Vehicle, public FourWheeler {
};
int main()
{
car obj;
return 0;
}
Output:
This is a Vehicle
This is a 4 wheeler Vehicle

c) Description:
In this type of inheritance, a derived class is created from another derived
class, we need to access it through the scope resolution operator.

MultilevelInherit.cpp:

MVGR College of engineering Autonomous 30


20331A0517 Object Oriented Programming Lab CSE-2A

#include<iostream>
using namespace std;
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
class fourWheeler: public Vehicle
{ public:
fourWheeler()
{
cout << "Objects with 4 wheels are vehicles\n";
}
};
class Car: public fourWheeler {
public:
Car()
{
cout << "Car has 4 Wheels\n";
}
};
int main()
{
Car obj;
return 0;
}
Output:
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels

MVGR College of engineering Autonomous 31


20331A0517 Object Oriented Programming Lab CSE-2A

d) Description:

In this type of inheritance, more than one subclass is inherited from a single
base class. i.e. more than one derived class is created from a single base
class, we need to access it through the scope resolution operator.

HierarchicalInherit.cpp:

#include<iostream>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle\n";
}
};
class Car: public Vehicle {
};
class Bus: public Vehicle {
};
int main() {
Car obj1;
Bus obj2;
return 0;
}
Output:
This is a Vehicle
This is a Vehicle

MVGR College of engineering Autonomous 32


20331A0517 Object Oriented Programming Lab CSE-2A

e) Description:
Hybrid Inheritance is implemented by combining more than one type of
inheritance. For example: Combining Hierarchical inheritance and Multiple
Inheritance, we need to access it through the scope resolution operator.

HybridInherit.cpp:
#include<iostream>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle\n";
}
};
class Fare {
public:
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

f) Description:

MVGR College of engineering Autonomous 33


20331A0517 Object Oriented Programming Lab CSE-2A

 In
single inheritance, subclasses inherit the features of one superclass. In the
image below, class A serves as a base class for the derived class B.

SingleInherit.java:
class A {
 int a, b;
 void display() {
 System.out.println(“Inside class A values =”+a+” ”+b);
 }
}
class B extends A {
 int c;
 void show() {
 System.out.println(“Inside Class B values=”+a+” “+b+” “+c);  }
}
class SingleInheritance {
 public static void main(String args[]) {
 B obj = new B();
 obj.a=10;
 obj.b=20;
 obj.c=30;
 obj.display();
 obj.show();
 }
}
Output:
Inside class A values =10 20
Inside Class B values=10 20 30

g) Description:
 In Multiple inheritances, one class can have more than one superclass and
inherit features from all parent classes. Please note that Java
does not support classes. In java, we can achieve multiple inheritances only.
In the image below, Class C is derived from iinterfaces A and B.

MultipleInherit.java:

MVGR College of engineering Autonomous 34


20331A0517 Object Oriented Programming Lab CSE-2A

import java.io.*;
import java.lang.*;
import java.util.*;
interface one {
public void print_geek();
}
interface two {
public void print_for();
}
interface three extends one, two {
public void print_geek();
}
class child implements three {
@Override public void print_geek()
{
System.out.println("o");
}

public void print_for() { System.out.println("pp"); }


}
public class Main {
public static void main(String[] args)
{
child c = new child();
c.print_geek();
c.print_for();
c.print_geek();
}
}
Output:
oppo

h) Description:

MVGR College of engineering Autonomous 35


20331A0517 Object Oriented Programming Lab CSE-2A

In Hierarchical Inheritance, one class serves as a superclass (base class) for


more than one subclass. In the below image, class A serves as a base class for
the derived class B, C and D.

HierarchicalInherit.java:
class A {
public void print_A() { System.out.println("Class A"); }
}
class B extends A {
public void print_B() { System.out.println("Class B"); }
}
class C extends A {
public void print_C() { System.out.println("Class C"); }
}
class D extends A {
public void print_D() { System.out.println("Class D"); }
}
public class Test {
public static void main(String[] args)
{
B obj_B = new B();
obj_B.print_A();
obj_B.print_B();
C obj_C = new C();
obj_C.print_A();
obj_C.print_C();
D obj_D = new D();
obj_D.print_A();
obj_D.print_D();
}
}
Output:
Class A
Class B

MVGR College of engineering Autonomous 36


20331A0517 Object Oriented Programming Lab CSE-2A

Class A
Class C
Class A
Class D

i) Description:
In Multilevel Inheritance, a derived class will be inheriting a base class and as
well as the derived class also act as the base class to other class. In the below
image, class A serves as a base class for the derived class B, which in turn
serves as a base class for the derived class C .

MultilevelInherit.java:
import java.io.*;
import java.lang.*;
import java.util.*;
class one {

public void print_geek()


{
System.out.println("o");
}
}
class two extends one {
public void print_for() { System.out.println("p"); }
}
class three extends two {
public void print_geek()
{

System.out.println("ps");
}
}

MVGR College of engineering Autonomous 37


20331A0517 Object Oriented Programming Lab CSE-2A

public class Main {


public static void main(String[] args)
{
three g = new three();
g.print_geek();
g.print_for();
g.print_geek();
}
}
Output:
o
p
ps

2) Write a program using Method Overriding & super keyword in CPP, JAVA.

a) MethodOR.cpp:
Description:
It is the redefinition of base class function in its derived class with the same
signature i.e return type and parameters. : Overriding is needed when the
derived class function has to do some added or different job than the base
class function.

#include<iostream>
using namespace std;
class BaseClass
{
public:
virtual void Display()
{
cout << "\nThis is Display() method"
" of BaseClass";
}

MVGR College of engineering Autonomous 38


20331A0517 Object Oriented Programming Lab CSE-2A

void Show()
{
cout << "\nThis is Show() method "
"of BaseClass";
}
};
class DerivedClass : public BaseClass
{
public:
// Overriding method - new working of
// base class's display method
void Display()
{
cout << "\nThis is Display() method"
" of DerivedClass";
}
};
int main()
{
DerivedClass dr;
BaseClass &bs = dr;
bs.Display();
dr.Show();
}
Output:
This is Display() method of DerivedClass
This is Show() method of BaseClass

b) MethodOR.java:
Description:
 Overriding is a feature that allows a subclass or child class to provide a
specific implementation of a method that is already provided by one of its
super-classes or parent classes. When a method in a subclass has the same
name, same parameters or signature, and same return type(or sub-type) as a

MVGR College of engineering Autonomous 39


20331A0517 Object Oriented Programming Lab CSE-2A

method in its super-class, then the method in the subclass is said


to override the method in the super-class.

class Parent {
void show()
{
System.out.println("Parent's show()");
}
}
class Child extends Parent {
@Override
void show()
{
System.out.println("Child's show()");
}
}
class Main {
public static void main(String[] args)
{
Parent obj1 = new Parent();
obj1.show();
Parent obj2 = new Child();
obj2.show();
}
}
Output:
Parent's show()
Child's show()

c) Super.CPP:
Description:
super keyword can also be used to access the parent class constructor. One
more important thing is that, ‘’super’ can call both parametric as well as non
parametric constructors depending upon the situation.

MVGR College of engineering Autonomous 40


20331A0517 Object Oriented Programming Lab CSE-2A

Code:
class Person
{
Person()
{
System.out.println("Person class Constructor");
}
}
class Student extends Person
{
Student()
{
super();
System.out.println("Student class Constructor");
}
}
class Test
{
public static void main(String[] args)
{
Student s = new Student();
}
}
Output:
Person class Constructor
Student class Constructor

Super.java:

Description:
 super keyword can also be used to access the parent class constructor. One
more important thing is that, ‘’super’ can call both parametric as well as non
parametric constructors depending upon the situation.
Code:

MVGR College of engineering Autonomous 41


20331A0517 Object Oriented Programming Lab CSE-2A

class Person
{
Person()
{
System.out.println("Person class Constructor");
}
}
class Student extends Person
{
Student()
{
super();
System.out.println("Student class Constructor");
}
}
class Test
{
public static void main(String[] args)
{
Student s = new Student();
}
}

Output:
Person class Constructor
Student class Constructor

3) Write a program of Type casts in CPP and JAVA, “this” & “final” keywords in CPP
and JAVA, access specifiers in CPP and JAVA.

a) typeCast.cpp:
Description:

MVGR College of engineering Autonomous 42


20331A0517 Object Oriented Programming Lab CSE-2A

 Typecasting refers to the conversion of one data type to another in a program.


Typecasting can be done in two ways: automatically by the compiler and
manually by the programmer or user. Type Casting is also known as Type
Conversion.

Code:
#include <iostream>
using namespace std;
int main ()
{
short x = 200;
int y;
y = x;
cout << " Implicit Type Casting " << endl;
cout << " The value of x: " << x << endl;
cout << " The value of y: " << y << endl;
int num = 20;
char ch = 'a';
int res = 20 + 'a';
cout << " Type casting char to int data type ('a' to 20): " << res << endl;
float val = num + 'A';
cout << " Type casting from int data to float type: " << val << endl;
return 0;
}
Output:
The value of x: 200
The value of y: 200
Typecasting char to int data type ('a' to 20): 117
Typecasting from int data to float type: 85

b) typeCast.java:
Description:
Type Casting is done during the program design time by the programmer.
Typecasting also refers to Narrow Conversion. Because in many cases, We

MVGR College of engineering Autonomous 43


20331A0517 Object Oriented Programming Lab CSE-2A

have to Cast large datatype values into smaller datatype values according to
the requirement of the operations.

Code:
import java.io.*;
public class GFG {
public static void main(String[] args)
{
int a = 3;
double db = (double)a;
System.out.println(db);
int db1 = (int)db;
System.out.println(db1);
}
}
Output:
3.0
3
a) this.java:
Description:
‘this’ is a reference variable that refers to the current object.
Code:
class A
{
B obj;
A(B obj)
{
this.obj = obj;
obj.display();
}
}
class B
{
int x = 5;

MVGR College of engineering Autonomous 44


20331A0517 Object Oriented Programming Lab CSE-2A

B()
{
A obj = new A(this);
}

void display()
{
System.out.println("Value of x in Class B : " + x);
}

public static void main(String[] args) {


B obj = new B();
}
}
Output:
Value of x in Class B : 5
b) final.cpp:
Description:
Sometimes you don’t want to allow derived class to override the base class’
virtual func
Code:
#include <iostream>
using namespace std;
class Base
{
public:
virtual void myfun() final
{
cout << "myfun() in Base";
}
};
class Derived : public Base
{
void myfun()
{

MVGR College of engineering Autonomous 45


20331A0517 Object Oriented Programming Lab CSE-2A

cout << "myfun() in Derived\n";


}
};
int main()
{
Derived d;
Base &b = d;
b.myfun();
return 0;
}
Output:
prog.cpp:14:10: error: virtual function ‘virtual void Derived::myfun()’
void myfun()
^
prog.cpp:7:18: error: overriding final function ‘virtual void Base::myfun()’
virtual void myfun() final

c) final.java:
Description:
When a variable is declared with the final keyword, its value can’t be
modified, essentially, a constant. This also means that you must initialize a
final variable. If the final variable is a reference, this means that the variable
cannot be re-bound to reference another object, but the internal state of the
object pointed by that reference variable can be changed i.e. you can add or
remove elements from the or final collection.

Code:
class GFG {
public static void main(String[] args)
{
int arr[] = { 1, 2, 3 };
for (final int i : arr)
System.out.print(i + " ");
}

MVGR College of engineering Autonomous 46


20331A0517 Object Oriented Programming Lab CSE-2A

}
Output:
123

d) accesSpecifiers.cpp:
Description:
Access Modifiers or Access Specifiers in a class are used to assign the
accessibility to the class members. That is, it sets some restrictions on the
class members not to get directly accessed by the outside functions.
Code:
#include<iostream>
using namespace std;
class Circle
{
public:
double radius;

double compute_area()
{
return 3.14*radius*radius;
}
};
int main()
{
Circle obj;

obj.radius = 5.5;
cout << "Radius is: " << obj.radius << "\n";
cout << "Area is: " << obj.compute_area();
return 0;
}
Output:
Radius is: 5.5

MVGR College of engineering Autonomous 47


20331A0517 Object Oriented Programming Lab CSE-2A

Area is: 94.985


e) accesSpecifiers.java:
Description:
As the name suggests access modifiers in Java helps to restrict the scope of a
class, constructor, variable, method, or data membe
Code:
package p1;
class A
{
private void display()
{
System.out.println("GeeksforGeeks");
}
}
class B
{
public static void main(String args[])
{
A obj = new A();
// Trying to access private method
// of another class
obj.display();
}
}
Output:
Compile time error

4) Write a program of Static Polymorphism using inheritance – C++.

StaticPoly.CPP:
Description:
The word polymorphism means having many forms. In simple words, we can
define polymorphism as the ability of a message to be displayed in more than

MVGR College of engineering Autonomous 48


20331A0517 Object Oriented Programming Lab CSE-2A

one form. A real-life example of polymorphism, a person at the same time can
have different characteristics. 
Code:
#include <bits/stdc++.h>
using namespace std;
class Geeks
{
public:
void func(int x)
{
cout << "value of x is " << x << endl;
}
void func(double x)
{
cout << "value of x is " << x << endl;
}
void func(int x, int y)
{
cout << "value of x and y is " << x << ", " << y << endl;
}
};
int main() {
Geeks obj1;
obj1.func(7);
obj1.func(9.132);
obj1.func(85,64);
return 0;

MVGR College of engineering Autonomous 49


20331A0517 Object Oriented Programming Lab CSE-2A

}
Output:
value of x is 7
value of x is 9.132
value of x and y is 85, 64

5) Write a program of Dynamic Polymorphism in CPP JAVA.

a) DynamicPoly.cpp:
Description:
the other hand occurs when a derived class has a definition for one of the
member functions of the base class. That base function is said to
be overridden.
Code:
#include <bits/stdc++.h>
using namespace std;
class base
{
public:
virtual void print ()
{ cout<< "print base class" <<endl; }
void show ()
{ cout<< "show base class" <<endl; }
};
class derived:public base
{
public:
void print () //print () is already virtual function in derived class, we could
also declared as virtual void print () explicitly
{ cout<< "print derived class" <<endl; }
void show ()
{ cout<< "show derived class" <<endl; }
};

MVGR College of engineering Autonomous 50


20331A0517 Object Oriented Programming Lab CSE-2A

int main()
{
base *bptr;
derived d;
bptr = &d;

//virtual function, binded at runtime (Runtime polymorphism)


bptr->print();

// Non-virtual function, binded at compile time


bptr->show();

return 0;
}
Output:
print derived class
show base class

b) DynamicPoly.java
Description:
It is a process in which a function call to the overridden method is resolved at
Runtime. This type of polymorphism is achieved by Method Overriding.
code:
class Parent {
void Print()
{
System.out.println("parent class");
}
}
class subclass1 extends Parent {
void Print() { System.out.println("subclass1"); }
}
class subclass2 extends Parent {
void Print()

MVGR College of engineering Autonomous 51


20331A0517 Object Oriented Programming Lab CSE-2A

{
// Print statement
System.out.println("subclass2");
}
}
class GFG {
public static void main(String[] args)
{
Parent a;
a = new subclass1();
a.Print();
a = new subclass2();
a.Print();
}
}
Output:
subclass1
subclass2
Conclusion:
In this week I had learned about:
 Different types of inheritance in C++ and java.
 Method overriding and usage of super keyword in C++ and java.
 Different models of typecasting in C++ and java.
 Usage and differences of this and final keywords in C++ and java.
 Programs on static and dynamic polymorphism.

MVGR College of engineering Autonomous 52


20331A0517 Object Oriented Programming Lab CSE-2A

Week – 5
Aim: To learn about multiple inheritances in C++ and Java.
1. Write a CPP program to show abstract classes and pure abstract classes.

Description 1:

An abstract class is, conceptually, a class that cannot be instantiated and is usually
implemented as a class that has one or more pure virtual (abstract) functions.
A pure virtual function is one that must be overridden by any concrete (i.e., non-
abstract) derived class. This is indicated in the declaration with the syntax " = 0" in
the member function's declaration.

a) Program:
#include<iostream>
using namespace std;
class A
{
public:
int x;
virtual void test() = 0;
};
class B : public A
{
public:
B(int y)
{
x = y;
}
void test()
{
std::cout<<"Hello I am virtual running in derived class and the number is
"<<x<<endl;
}
};

MVGR College of engineering Autonomous 53


20331A0517 Object Oriented Programming Lab CSE-2A

int main(void)
{
B obj(4);
obj.test();
return 0;
}

Output:
Hello I am virtual running in derived class and the number given 4

Description 2
An abstract class is one in which there is a declaration but no definition for a
member function. The way this concept is expressed in C++ is to have the member
function declaration assigned to zero.

b) Program :

#include<iostream>
using namespace std;
class A
{
public:
virtual void test() = 0;
};
class B : public A
{
public:
void test()
{
std::cout<<"Hello I am virtual running in derived class "<<endl;
}
};

int main(void)
{

MVGR College of engineering Autonomous 54


20331A0517 Object Oriented Programming Lab CSE-2A

B obj;
obj.test();
return 0;
}

Output:
Hello I am virtual running in the derived class

2. Write a program using Abstract classes in java, the final keyword.

Description:
A class that is declared with the abstract keyword is known as an abstract class in
java. It can have abstract and non-abstract methods (method with the body).
An abstract method should be overwritten. But, once a method is written with the
final keyword we can’t overwrite it.

1. Program:
abstract class Shape{  
abstract void draw();  
}  
//In real scenario, implementation is provided by others i.e. unknown by end u
ser  
class Rectangle extends Shape{  
void draw(){System.out.println("drawing rectangle");}  
}  
class Circle1 extends Shape{  
void draw(){System.out.println("drawing circle");}  
}  
//In real scenario, method is called by programmer or user  
class TestAbstraction1{  
public static void main(String args[]){  
Shape s=new Circle1();//In a real scenario, object is provided through method
s.draw();  
}  
}  

Output:

MVGR College of engineering Autonomous 55


20331A0517 Object Oriented Programming Lab CSE-2A

drawing circle

2. Program:
class Bike{  
  final void run(){System.out.println("running");}  
}  
     
class Honda extends Bike{  
   void run(){System.out.println("running safely with 100kmph");}  
     
   public static void main(String args[]){  
   Honda honda= new Honda();  
   honda.run();  
}  

Output:
Compile time error.

3. Write a program using interfaces in java.

Description:
In java, we need to write them in a class and those methods or attributes can be
accessed through the class which is having the main class. Also the name of the
program should be same as the main class name.

Program:
interface printable
{
void print();
}
class A6 implements printable
{
public void print()
{
System.out.println("Hello");
}

MVGR College of engineering Autonomous 56


20331A0517 Object Oriented Programming Lab CSE-2A

public static void main(String args[])


{
A6 obj = new A6();
obj.print();
}
}
Output:
Hello

CONCLUSION:
The main 2 points from this I have learned is that abstract classes have features that
can’t be instantiated by themselves but, can be done by their derived classes.
Interfaces in java can be considered as templates, these templates can be used in
how many ever classes we want.

MVGR College of engineering Autonomous 57


20331A0517 Object Oriented Programming Lab CSE-2A

Week – 6
Aim: To study packages and templates in Java.

1) Write a Java Program to call and access packages from the outside folder,
Outside of the drive
Description:
Java package is a mechanism of grouping similar types of classes, interfaces, and
sub-classes collectively based on functionality. Creating a package in Java is a very
easy task. Choose a name for the package and include a package command as the
first statement in the Java source file.
Compare.java:
package MyPackage;
public class Compare {
  int num1, num2;
  Compare(int n, int m) {
    num1 = n;
    num2 = m;
 }
  public void getmax(){
    if ( num1 > num2 ) {
      System.out.println("Maximum value of two numbers is " + num1);
  }
    else {
      System.out.println("Maximum value of two numbers is " + num2);
  }
 }
  public static void main(String args[]) {

MVGR College of engineering Autonomous 58


20331A0517 Object Oriented Programming Lab CSE-2A

    Compare current[] = new Compare[3];


     
    current[1] = new Compare(5, 10);
    current[2] = new Compare(123, 120);
    for(int i=1; i < 3 ; i++) {
      current[i].getmax();
  }
 }
}
Demo.java:
import MyPackage.Compare;

public class Demo{


   public static void main(String args[]) {
      int n=10, m=10;
      Compare current = new Compare(n, m);
      if(n != m) {
         current.getmax();
   }
      else {
         System.out.println("Both the values are same");
   } 
   }
}
Compilation:
C:\Users\RaviV\Desktop\VSC\Java> javac -d . Compare.java

MVGR College of engineering Autonomous 59


20331A0517 Object Oriented Programming Lab CSE-2A

Output:
Maximum value of two numbers is 10
Maximum value of two numbers is 123
Both the values are same

2) Write a CPP program using Template functions & classes in C++.


Description:
In C++ we created templates in functions and classes such that we can use
different classes with only one class code and functions with any methods.
a) TempFunc.cpp:

Template<class T>
Void swap(T &a,T &b){
T temp = a;
a = b;
b = temp;
}
Template<class T1 class T2>
Float Avg(T1 a,T2 b){
Float avg =(a+b)/2.0;
Return 0;
}
Int main(){
Float a;
a = Avg(5,2.4);
printf(“The avg is %f\n”,a);
int x=5,y=7;
swap(x,y);
cout<<x<<endl<<y;
}
Output:
The avg is 3.7
7
5

MVGR College of engineering Autonomous 60


20331A0517 Object Oriented Programming Lab CSE-2A

b) TempClass.cpp:

#include <iostream>
using namespace std;
template <class T>
class Number {
private:
T num;
public:
Number(T n) : num(n) {}

T getNum() {
return num;
}
};

int main() {
Number<int> numberInt(7);
Number<double> numberDouble(7.7);
cout << "int Number = " << numberInt.getNum() << endl;
cout << "double Number = " << numberDouble.getNum() << endl;
return 0;
}
Output:
int number = 7
double number = 7.7

Conclusion:
In this week I had learned about:
 How to import methods in a package to another java program.
 How to create a user-defined package in java.
 How to save a user-defined package in a particular location of our system.
 How to create the template for class and function.
 How to declare template class.

MVGR College of engineering Autonomous 61

You might also like