Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Object Oriented Programming Practical File, Study notes of Object Oriented Programming

Object Oriented Programming Practical File for Students

Typology: Study notes

2018/2019

Uploaded on 10/01/2019

anmol-sethi
anmol-sethi 🇮🇳

5

(1)

2 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1.[Classes and Objects] Write a program that uses a class where the member functions are
defined inside a class.
//Program to illustrate working of Objects and Class in C++ Programming
#include <iostream>
using namespace std;
class temp {
private:
int data1;
float data2;
public:
void int_data(int d){
data1=d;
cout<<"Number: "<<data1;
}
float float_data(){
cout<<"\nEnter data: ";
cin>>data2;
return data2;
}
};
int main(){
temp obj1, obj2;
obj1.int_data(12);
cout<<"You entered "<<obj2.float_data();
return 0;
}
Output:
Number: 12
Enter data: 12.43
You entered: 12.43
2. [Classes and Objects] Write a program that uses a class where the member functions
are defined outside a class.
#include <iostream>
using namespace std;
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
// Member functions declaration
double getVolume(void);
void setLength( double len );
void setBreadth( double bre );
void setHeight( double hei );
};
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Object Oriented Programming Practical File and more Study notes Object Oriented Programming in PDF only on Docsity!

1.[Classes and Objects] Write a program that uses a class where the member functions are

defined inside a class.

//Program to illustrate working of Objects and Class in C++ Programming

#include

using namespace std; class temp {

private: int data1; float data2; public: void int_data(int d){ data1=d; cout<<"Number: "<<data1; } float float_data(){ cout<<"\nEnter data: "; cin>>data2; return data2; } }; int main(){ temp obj1, obj2; obj1.int_data(12); cout<<"You entered "<<obj2.float_data(); return 0; }

Output:

Number: 12 Enter data: 12. You entered: 12.

2. [Classes and Objects] Write a program that uses a class where the member functions

are defined outside a class.

#include using namespace std; class Box {

public: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box

// Member functions declaration double getVolume(void); void setLength( double len ); void setBreadth( double bre ); void setHeight( double hei ); };

// Member functions definitions

double Box::getVolume(void) { return length * breadth * height; }

void Box::setLength( double len ) { length = len; }

void Box::setBreadth( double bre ) { breadth = bre; }

void Box::setHeight( double hei ) { height = hei; }

// Main function for the program int main( ) {

Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box double volume = 0.0; // Store the volume of a box here

// box 1 specification Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0);

// box 2 specification Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0);

// volume of box 1 volume = Box1.getVolume(); cout << "Volume of Box1 : " << volume <<endl;

// volume of box 2 volume = Box2.getVolume(); cout << "Volume of Box2 : " << volume <<endl; return 0; }

Output :

Volume of Box1 : 210 Volume of Box2 : 1560

3. [Constructors and Destructors] Write a program to demonstrate the use of zero

argument and parameterized constructors.

cin>>a>>b; } complex operator+(complex ob) { complex t; t.a=a+ob.a; t.b=b+ob.b; return(t); } complex operator-(complex ob) { complex t; t.a=a-ob.a; t.b=b-ob.b; return(t); } void display() { cout<<a<<"+"<<b<<"i"<<"\n"; } };

void main() { clrscr(); complex obj1,obj2,result,result1;

obj1.getvalue(); obj2.getvalue();

result = obj1+obj2; result1=obj1-obj2;

cout<<"Input Values:\n"; obj1.display(); obj2.display();

cout<<"Result:"; result.display(); result1.display();

getch(); }

Output: Enter the value of Complex Numbers a, b 4 5 Enter the value of Complex Numbers a, b 2 2 Input Values 4 + 5i 2 + 2i Result 6 + 7i

2 + 3i

5. [Typecasting] Write a program to demonstrate the typecasting of basic type to class

type.

//Same as calling fuction from main to class

Class a { Void a() { Cout<<”I am construcot”; } Void show() { Cout<<”I am function”; } }; Void main() { A t; t.show(); }

Output :

I am construcot I am function

6. [Typecasting] Write a program to demonstrate the typecasting of class type to basic

type.

//Program Simple Class Example Program In C++

#include #include<conio.h> using namespace std;

class person {

public: string name; int number; };

int main() { // Object Creation For Class person obj;

//Get Input Values For Object Varibales cout<<"Enter the Name :"; cin>>obj.name;

square(); c=b*a; cout<<"\n\nCube :::\t"<<c; } };

int main() { clrscr(); bottom b1; b1.cube(); getch(); }

OUTPUT

8. [Inheritance] Write a program to demonstrate the multiple inheritance.

PROGRAM:

#include<iostream.h>

#include<conio.h>

class student { protected: int rno,m1,m2; public: void get() { cout<<"Enter the Roll no :"; cin>>rno; cout<<"Enter the two marks :"; cin>>m1>>m2; } }; class sports { protected: int sm; // sm = Sports mark public: void getsm() { cout<<"\nEnter the sports mark :"; cin>>sm;

} };

class statement:public student,public sports

{ int tot,avg; public: void display() { tot=(m1+m2+sm); avg=tot/3; cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal : "<<tot; cout<<"\n\tAverage : "<<avg; } }; void main() { clrscr(); statement obj; obj.get(); obj.getsm(); obj.display(); getch(); } Output: Enter the Roll no: 100

Enter two marks

90 80

Enter the Sports Mark: 90

Roll No: 100 Total : 260 Average: 86.

9. [Polymorphism] Write a program to demonstrate the runtime polymorphism.

class Base { public:

virtual void display(int i) { cout<<"Base::"<<i; }

};

class Derv: public Base { public:

void display(int j) { cout<<"Derv::"<<j; } };

ofstream outfile; outfile.open("afile.dat");

cout << "Writing to the file" << endl; cout << "Enter your name: "; cin.getline(data, 100);

// write inputted data into the file. outfile << data << endl;

cout << "Enter your age: "; cin >> data; cin.ignore();

// again write inputted data into the file. outfile << data << endl;

// close the opened file. outfile.close();

// open a file in read mode. ifstream infile; infile.open("afile.dat");

cout << "Reading from the file" << endl; infile >> data;

// write the data at the screen. cout << data << endl;

// again read the data from the file and display it. infile >> data; cout << data << endl;

// close the opened file. infile.close();

return 0; } output:

$./a.out Writing to the file Enter your name: Zara Enter your age: 9 Reading from the file Zara 9

The Process of Writing a C++ Program

Step 1: Write the source codes (.cpp) and header files (.h).

Step 2: Pre-process the source codes according to the preprocessor directives. Preprocessor directives begin with a hash sign (#), e.g., #include and #define. They indicate that certain manipulations (such as including another file or replacement of symbols) are to be performed BEFORE compilation.

Step 3: Compile the pre-processed source codes into object codes (.obj, .o).

Step 4: Link the compiled object codes with other object codes and the library object codes (.lib, .a) to produce the executable code (.exe).

Step 5: Load the executable code into computer memory.

Step 6: Run the executable code, with the input to produce the desried output.