0% found this document useful (0 votes)
16 views

oops practical 2024

The document contains multiple C++ programming examples demonstrating function overloading, file handling, inheritance, and constructors. Each section includes code snippets and sample input/output to illustrate the concepts. The programs cover printing different data types, writing and reading from files, creating a derived class, and using a copy constructor.

Uploaded by

agkhandvd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

oops practical 2024

The document contains multiple C++ programming examples demonstrating function overloading, file handling, inheritance, and constructors. Each section includes code snippets and sample input/output to illustrate the concepts. The programs cover printing different data types, writing and reading from files, creating a derived class, and using a copy constructor.

Uploaded by

agkhandvd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Q1.

Write a program in C++ to implement function overloading


Solution
#include <iostream.h>

class printData {
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(char* c) {
cout << "Printing character: " << c << endl;
}
};

int main(void) {
printData pd;

// Call print to print integer


pd.print(5);

// Call print to print float


pd.print(500.263);

// Call print to print character


pd.print("Hello C++");

return 0;
}
Output
Printing int: 5
Printing float: 500.263
Printing character: Hello C++
Q2. Write a program in C++ to implement data file handling.
#include< fstream.h>
int main () {
char data[100];

// open a file in write mode.


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;
}

sample input and output −


$./a.out
Writing to the file
Enter your name: Zara
Enter your age: 9
Reading from the file
Zara
9
Q3. Write a program in C++ to implement inheritance using class.
#include <iostream.h>
class Person {
int id;
char name[100];

public:
void set_p()
{
cout << "Enter the Id:";
cin >> id;
cout << "Enter the Name:";
cin >> name;
}

void display_p()
{
cout << endl <<"Id: "<< id << "\nName: " << name <<endl;
}
};

class Student : private Person {


char course[50];
int fee;

public:
void set_s()
{
set_p();
cout << "Enter the Course Name:";
cin >> course;
cout << "Enter the Course Fee:";
cin >> fee;
}

void display_s()
{
display_p();
cout <<"Course: "<< course << "\nFee: " << fee << endl;
}
};

int main()
{
Student s;
s.set_s();
s.display_s();
return 0;
}
Sample Output
Enter the Id: 101
Enter the Name: Dev
Enter the Course Name: GCS
Enter the Course Fee:70000

Id: 101
Name: Dev
Course: GCS
Fee: 70000
Q4. Write a program in oops to implement constructor.
#include<iostream.h>
#include<string.h>
class student
{
int rno;
char name[50];
double fee;
public:
student(int,char[],double);
student(student &t) //copy constructor (member wise initialization)
{
rno=t.rno;
strcpy(name,t.name);

}
void display();
void disp()
{
cout<<endl<<rno<<"\t"<<name;
}

};
student::student(int no, char n[],double f)
{
rno=no;
strcpy(name,n);
fee=f;
}

void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}

int main()
{
student s(1001,"Manjeet",10000);
s.display();

student manjeet(s); //copy constructor called


manjeet.disp();

return 0;
}

1001 Manjeet 10000


1001 Manjeet 10000

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy