Docsity

OBJECT ORIENTED PROGRAMMING LABORATORY, Study Guides, Projects, Research of Engineering

Toggle sidebar
bg1
CS 1262 OBJECT ORIENTED PROGRAMMING LABORATORY
AIM
To implement dynamic memory allocation, constructors, destructors, friend
function, inheritance and interfaces.
1. String concatenation using dynamic memory allocation concept.
2. Implementation of arithmetic operations on complex numbers
using constructor overloading.
3. To read a value of distance from one object and add with a value in another
object using friend function.
4. Implementation of + and - operator overloading and implementation of addition
operation of octal object with integer using operator overloading.
5. Implementation of addition and subtraction of two polynomial objects using
operator overloading.
6. Managing bank account using inheritance concept.
7. To compute the area of triangle and rectangle using inheritance and virtual
function.
8. Writing simple programs in Java.
9. Use of interfaces in Java.
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

Partial preview of the text

Download OBJECT ORIENTED PROGRAMMING LABORATORY and more Study Guides, Projects, Research Engineering in PDF only on Docsity!

CS 1262 OBJECT ORIENTED PROGRAMMING LABORATORY

AIM

To implement dynamic memory allocation, constructors, destructors, friend function, inheritance and interfaces.

1. String concatenation using dynamic memory allocation concept. 2. Implementation of arithmetic operations on complex numbers using constructor overloading.

  1. To read a value of distance from one object and add with a value in another object using friend function.
  2. Implementation of + and - operator overloading and implementation of addition operation of octal object with integer using operator overloading.
  3. Implementation of addition and subtraction of two polynomial objects using operator overloading. 6. Managing bank account using inheritance concept.

7. To compute the area of triangle and rectangle using inheritance and virtual

function.

  1. Writing simple programs in Java.
  2. Use of interfaces in Java.

DETAILED SYLLABUS

1. String concatenation using dynamic memory allocation concept

Aim To implement the string concatenation function by using dynamic memory allocation concept.

Objective To concatenate two or more strings into one string by allocating memory to objects at the time of their construction.

Exercises

  1. Create class STRING with two constructors. The first is an empty constructor, which allows declaring an array of strings. The second constructor initializes the length of the strings, and allocates necessary space for the string to be stored and creates the string itself.
  2. Create a member function to concatenate two strings.
  3. Estimate the combined length of the strings to be joined and allocates memory for the combined string using new operator and then creates the same using the string functions strcpy() and strcat().
  4. Display the concatenated string. 2. Implementation of arithmetic operations on complex numbers using constructor overloading

Aim To implement arithmetic operations on complex numbers using constructor overloading

Objective To represent complex numbers and to perform arithmetic operations on complex numbers using overloaded constructors in a class.

4. Implementation of + and - operator overloading and implementation of addition operation of octal object with integer using operator overloading

Aim To implement + and – operator overloading and to implement addition operation of octal object with integer using operator overloading.

Objective (A) To display the number of days between two valid dates and the date after a number of days from a valid date by overloading the operators + and -. (B)To represent octal numbers and to add an octal object with integer by overloading operator ā€˜+’.

Exercises (A)

  1. Create a class called DATE and define two member functions get-data and display-result.
  2. Accept two valid dates in the form of dd/mm/yyyy using get-data.
  3. Overload operators + and – to display the number of days between two valid dates using display-result.
  4. Repeat step 3 to display the date after a number of days from a valid date using display-result. (B)
  5. Create class OCTAL for representing octal numbers.
  6. Create a constructor to implement OCTAL h=x where x is an integer.
  7. Overload operator ā€˜+’ to perform the integer addition with an OCTAL object like int y= h+k (where h is an OCTAL object and k is an integer).
  8. Display the resultant integer value y.

5. Implementation of addition and subtraction of two polynomial objects using operator overloading

Aim To implement addition and subtraction operations of two polynomials and display using << operator overloading.

Objective To add and subtract two POLYNOMIAL objects and to display results by overloading the operator <<.

Exercises

  1. Create a class called POLYNOMIAL with constructors to create polynomial objects and to initialize with specific values.
  2. Create member functions to perform addition and subtraction of two polynomials.
  3. Overload operator << to display the results of addition and subtraction operations on two polynomials.
  4. Display the results. 6. Managing bank account using inheritance concept

Aim To manage the account information of the customer using inheritance concept.

Objective To maintain and update the customer account specific information using inheritance concept.

Exercises

  1. Create a class with the following member variables. Customer name, account number and account type.
  2. Create the derived classes with following member variables.

Writing simple programs in Java

Aim To generate random numbers using simple Java program

Objective To generate random numbers using built in function random().

Exercises

  1. Create a class called rand with variable declaration and includes header file math.
  2. Generate random numbers using built in function random().
  3. Display the result. 9. Use of Interfaces in Java

Aim To calculate area of rectangle and circle using interfaces.

Objective To create two classes and store the values of distance and to read the values from one class and add with a value in another object using friend function

Exercises

  1. Create two classes AB and AC and store the value of distances.
  2. Declare friend function.
  3. Read the value from the classes.
  4. Perform addition to one object of AB with another object of AC.
  5. Display the result of addition.

Pr. No.

STRING CONCATENATION

AIM:

To implement the string concatenation function by using dynamic memory allocation concept.

REQUIREMENTS: Hardware : Pentium IV PC Software : Turbo C++

PROGRAM: /*********************************************************************************************** Pr.No.1 Date: STRING CONCATENATION Name : Reg No. : Objective : To concatenate two or more strings into one string by allocating memory to objects at the time of their construction. ***********************************************************************************************/ #include<iostream.h> #include<string.h> #include<conio.h>

class string { char *name; int length; public: string() { length=0; name=new char[length+1];

name2.display(); name3.display(); s1.display(); s2.display(); getch(); }

OUTPUT: Object Oriented Programming Lab Object Oriented Object Oriented Programming Lab

Pr. No.

ARITHMETIC OPERATIONS ON COMPLEX NUMBERS

AIM:

To implement arithmetic operations on complex numbers using constructor overloading

REQUIREMENTS: Hardware : Pentium IV PC Software : Turbo C++

PROGRAM: /*********************************************************************************************** Pr.No.2 Date:

ARITHMETIC OPERATION ON COMPLEX NUMBERS Name : Reg.No. : Objective : To implement arithmetic operation on complex numbers using constructor over loading. ***********************************************************************************************/ #include<iostream.h> #include<math.h> #include<conio.h>

class complex { float x,y; public: complex(){} complex(float z) { x=y=z;

complex operator/ (complex c1,complex c2) { complex c3; float a; a=((c2.xc2.x)+(c2.yc2.y)); c3.x=((c1.xc2.x)+(c1.yc2.y))/a; c3.y=((c1.y/c2.x)-(c1.y*c2.y))/a; return c3; }

void show(complex c) { if(c.y<0) cout<<c.x<<"-j"<<fabs(c.y)<<endl; else cout<<c.x<<"+j"<<fabs(c.y)<<endl; }

void main() { complex A(1,2); complex B(3,1); complex C,D,E,F; clrscr(); C=A+B; D=A-B; E=A*B; F=A/B; cout<<"Complex No:1 = "; show(A); cout<<"Complex No:2 = "; show(B); cout<<"Sum = "; show(C); cout<<"Difference = ";

show(D); cout<<"Product = "; show(E); cout<<"Quotient = "; show(F); getch(); }

OUTPUT: Complex No:1 = 1+j Complex No:2 = 3+j Sum = 4+j Difference = -2+j Product = 1+j Quotient = 0.5-j0.

AB()

cout<<"Enter the first distance(in feet):"; cin>>d; } friend void add(AB,AC); };

class AC { float d; public: AC() { cout<<"Enter the second distance(in inches):"; cin>>d; }

friend void add(AB a,AC b) { float total; b.d=b.d/12; total=a.d+b.d; cout<<"Total Distance:"<<total<<"feet"; } }; void main() { clrscr(); AB A; AC B; add(A,B); getch(); }

OUTPUT:

Enter the first distance(in feet): Enter the second distance(in inches): Total Distance:14.5feet

Pr. No.

OPERATOR OVERLOADING AND

ADDITION OF OCTAL WITH INTEGER

AIM:

To implement + and – operator overloading and to implement addition operation of octal object with integer using operator overloading.

REQUIREMENTS: Hardware : Pentium IV PC Software : Turbo C++

PROGRAM: /************************************************************************************************ Pr.No.4A Date: IMPLEMENTATION OF OPERATOR OVERLOADING Name : Reg.No. : Objective : To display the number of days between two valid dates and to display the date after a number of days from a valid date by overloading the operator '+' and '-'. ***********************************************************************************************/ #include<iostream.h> #include<conio.h> class date { int day,date,month,year; public:

if(month==2&&isleapyear()) return 29; else return m[month-1]; } void operator+() { ++day; if(day>thismonthmax()) { day=1; month++; } if(month>12) { month=1; year++; } }

void operator-() { int i,d1,d2,dd,m1,m2,md,ms=0,y1,y2,yd,ys,tdays; cout<<"Enter date 1:"; cin>>d1>>m1>>y1; cout<<"Enter date 2:"; cin>>d2>>m2>>y2; int m[12]={31,28,31,30,31,30,31,31,30,31,30,31}; if(y2<y1) { cout<<"Invalid date"; } else { yd=y2-y1;

if(m2<m1) { m1=m1+12; y1--; md=m2-m1; } else { md=m2-m1; } if(d2<d1) { d1=d1+m[m1-1]; m1--; dd=d2-d1; } else { dd=d2-d1; } for(i=m1;i<m2;i++) { ms=ms+m[i]; } if((y2%400==0)||(y2%100!=0)&&(y2%1==0)) { ys=366yd; } else ys=365yd; tdays=dd+ms+ys; cout<<"DIFFERENCE="<<tdays; } };