Write a C++ program that inputs a character from keyboard and display its corresponding
ASCII value on the screen
// Question - 1
include<bits/stdc++.h>
using namespace std ;
int main()
{
char a ;
cin >> a ;
cout << "ASCII Value is --> " << (int) a << endl ;
return 0 ;
}
Write a C++ program using class called temperature and member functions for a
temperature in Fahrenheit and display it in Celsius.
include<bits/stdc++.h>
using namespace std ;
class temperature
{
private :
float farhen , c ;
public :
float conversion(float f)
{
farhen = f ;
c = (f-32) * (5.0/9.0) ;
return c ;
}
};
int main()
{
temperature t ;
float f ;
cin >> f ;
cout << "Temperature in Celsius --> " << t.conversion(f) << endl ;
}
Write a C++ program function using reference variables as arguments to swap the values of
a pair of integers
// Question - 3
include<bits/stdc++.h>
using namespace std ;
class q2
{
private :
int t ;
public:
void swap(int a , int b )
{
t = a ;
a = b ;
b = t ;
}
} ;
int main()
{
int a , b ;
q2 sa ;
cin >> a >> b ;
sa.swap( &a , &b ) ;
cout << a << " " << b << endl ;
}
4.Write a C++ program which explains and show different storage classes auto, extern static
and register.
[4:42 pm, 19/11/2022] @#₹_%&-+(): #include
using namespace std;
int staticFunc()
{
cout << "For Static Variables: ";
static int count = 0;
count++;
return count;
}
int nonStaticFunc()
{
cout << "For Non-Static Variables: ";
int count = 0;
count++;
return count;
}
void registerStorageClass()
{
cout << "Register class\n";
register char b = 'G';
cout << "Value of the variable 'b'"
<< " declared as register: " << b;
}
int x;
void externStorageClass()
{
cout << "Extern class\n";
extern int x;
cout << "Value of the variable 'x'"
<< "declared, as extern: " << x << "\n";
x = 2;
cout
<< "Modified value of the variable 'x'"
<< " declared as extern: "
<< x << endl;
}
void autoStorage()
{
cout << "Auto Class;\n" << endl;
auto a = 69;
auto b = 4.20;
auto c = 'F';
auto d = "IIIT Bhagalpur";
cout << a << " \n";
cout << b << " \n";
cout << c << " \n";
cout << d << " \n";
}
[4:42 pm, 19/11/2022] @#₹_%&-+(): #include
using namespace std;
int staticFunc()
{
cout << "For Static Variables: ";
static int count = 0;
count++;
return count;
}
int nonStaticFunc()
{
cout << "For Non-Static Variables: ";
int count = 0;
count++;
return count;
}
void registerStorageClass()
{
cout << "Register class\n";
register char b = 'G';
cout << "Value of the variable 'b'"
<< " declared as register: " << b;
}
int x;
void externStorageClass()
{
cout << "Extern class\n";
extern int x;
cout << "Value of the variable 'x'"
<< "declared, as extern: " << x << "\n";
x = 2;
cout
<< "Modified value of the variable 'x'"
<< " declared as extern: "
<< x << endl;
Write a C++ program to find factorial of number using function
include<bits/stdc++.h>
using namespace std ;
int fact(int n)
{
int facto = 1 ;
for (int i = 1 ; i <= n ; i++)
facto *= i ;
return facto ;
}
int main()
{
int n ;
cin >> n ;
cout << fact(n) << endl ;
}
Write a C++ program to check number is palindrome or not using Function.
include<bits/stdc++.h>
using namespace std ;
define cty cout<<"YES A PALINDROME\n"
define ctn cout<<"NOT A PALINDROME\n"
bool chk(int n)
{
int og = n ;
int rev = 0 ;
while(n != 0)
{
int rem = n % 10 ;
rev = rev*10 + rem ;
n /= 10 ;
}
if (og == rev)
return true ;
else
return false ;
}
int main()
{
int n ;
cin >> n ;
if (chk(n))
cty ;
else
ctn ;
}
Write a C++ Program to find Cube of a Number using function.
QUESTION --> 3
include<bits/stdc++.h>
using namespace std ;
int cube(int n)
return nnn ;
int main()
{
int n ;
cin >> n ;
cout << cube(n) << endl ;
}
Write a C++ Program to Swap two numbers and characters using call by value.
// QUESTION --> 4
include<bits/stdc++.h>
using namespace std ;
void swap(int x , int y)
{
int z = x ;
x = y ;
y = z ;
cout << "Swapped Values " << x << " " << y << endl ;
}
int main()
{
int a , b ;
cin >> a >> b ;
cout << "Original Values " << a << " " << b << endl ;
swap(a,b) ;
}
Write a C++ program to print the following output using for loops.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
//Question -- 1
include<bits/stdc++.h>
using namespace std ;
int main()
{
for (int i = 1 ; i < 6 ; i++){
for (int j = 1 ; j <= i ; j++){
cout << i << " " ;
}
cout << endl ;
}
return 0 ;
}
Write a program in C++ to input a single digit number and print a rectangular form of 4
columns and 6 rows.
5 5 5 5
5 5
5 5
5 5
5 5
5 5 5 5
include<bits/stdc++.h>
using namespace std ;
int main(){
int n ;
cin >> n ;
for (int i = 0 ; i < 6 ; i++){
for (int j = 0 ; j < 4 ; j++){
if (j == 0 || j == 3 || i == 0 || i == 5)
cout << n << " " ;
else
cout << " " ;
}
cout << endl ;
}
}
Write a C++ program to display the current date and time.
// Question -- 3
include<bits/stdc++.h>
using namespace std ;
int main(){
time_t timetoday ;
time (&timetoday) ;
cout << "Current Date and Time --> " << asctime(localtime(&timetoday)) << endl ;
return 0 ;
}
Write a program in C++ to enter P, T, R and calculate Simple Interest.
// Question -- 4
include<bits/stdc++.h>
using namespace std ;
int main(){
int p , r , t ;
cout << "Enter Principal --> " ;
cin >> p ;
cout << "Enter Rate of Interest --> " ;
cin >> r ;
cout << "Enter Time Period --> " ;
cin >> t ;
int si = prt/100 ;
cout << "Simple Interest Calculated is --> " << si << endl ;
return 0 ;
}
Write a C++ program for unary minus (-) operator overloading
// Question - 1
include
using namespace std ;
class cls{
private:
int n ;
public:
void get(int x)
{
n = x ;
}
void prt()
{
cout << "The Value of n is -> " << n << endl ;
}
void operator - (void)
{
n = -n ;
}
};
int main(){
cls op ;
op.get(69);
-op ;
op.prt() ;
return 0 ;
}
Write a C++ program for unary increment (++) and decrement (--) operator overloading
// Question - 2
include
using namespace std ;
class cls{
private:
int n ;
public:
void get(int x)
{
n = x ;
}
void prt()
{
cout << n << endl ;
}
void operator-- (void) // unary decrement overloading
{
n = --n ;
}
void operator++ (void) // unary increment overloading
{
n = ++n ;
}
};
Write a C++ Program to Display Date using Constructors
include
using namespace std;
class date
{
private:
int dd, mm, yy;
public:
date()
{
dd=31;
mm=12;
yy=2016;
cout<<"\nDate Object has been created..............\n";
}
void display()
{
cout<<"\nThe Entered Date is :: ";
cout<<dd<<"-"<<mm<<"-"<<yy<<"\n";
}
};
int main ()
{
date date1;
date1.display ();
return 0;
}
C++ program to Display Student Details using constructor and destructor
include
using namespace std;
class stu
{
private:
char name[20],add[20];
int roll,zip;
public:
stu ();//Constructor
~stu();//Destructor
void read();
void disp();
};
stu :: stu()
{
cout<<"\nThis is Student Details constructor called..........."<<endl;
}
void stu :: read()
{
cout<<"\nEnter the student Name :: ";
cin>>name;
cout<<"\nEnter the student roll no :: ";
cin>>roll;
cout<<"\nEnter the student address :: ";
cin>>add;
cout<<"\nEnter the Zipcode :: ";
cin>>zip;
}
void stu :: disp()
{
cout<<"\nThe Entered Student Details are shown below ::---------- \n";
cout<<"\nStudent Name :: "<<name<<endl;
cout<<"\nRoll no is :: "<<roll<<endl;
cout<<"\nAddress is :: "<<add<<endl;
cout<<"\nZipcode is :: "<<zip;
}
stu :: ~stu()
{
cout<<"\n\nStudent Detail is Closed.............\n";
}
int main()
{
stu s;
s.read ();
s.disp ();
return 0;
}
C++ Program for Constructor with Parameters (Parameterized Constructor)
include
using namespace std;
class ParamA {
private:
int b, c;
public:
ParamA (int b1, int c1)
{
b = b1;
c = c1;
}
int getX ()
{
return b;
}
int getY ()
{
return c;
}
};
int main ()
{
ParamA p1(10, 15);
cout << "p1.b = " << p1. getX() << ", p1.c = " << p1.getY();
return 0;
}
C++ Program to calculate Volume of Box using Constructor.
include
using namespace std;
class box
{
double length,width,height;
double volume;
box::box(double a,double b,double c)
{
length=a;
width=b;
height=c;
volume=lengthwidthheight;
}
void box::vol()
{
cout<<"\nDimensions of Box are :: \n";
cout<<"\nLength of Box :: "<<length<<"\n";
cout<<"\nWidth of Box :: "<<width<<"\n";
cout<<"\nHeight of Box :: "<<height<<"\n";
cout<<"\nVolume of Box :: "<<volume<<"\n";
}
int main()
{
box x(2.4,5.7,2.1),y(3.3,4.4,5.5);
x.vol();
y.vol();
return 0;
}
Write a C++ program to read and print employee
information with department and pf information using
hierarchical inheritance.
/
C++ program to read and print employee information with department
and pf information using hierarchical inheritance.
/
include
include
using namespace std;
//Base Class - basicInfo
class basicInfo {
protected:
char name[30];
int empId;
char gender;
int main()
{
result R;
R.get_no(34);
R.get_submarks(67.2);
R.get_spmarks(98.9);
R.put_result();
return 0;
}
Write a C++ Program that illustrate single inheritance.
include
using namespace std;
class Account {
public:
float salary = 60000;
};
class Programmer: public Account {
public:
float bonus = 5000;
};
int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
Write a C++ Program that illustrate multipe inheritance
include
using namespace std;
class A
{
public:
A() { cout << "A's constructor called" << endl; }
};
class B
{
public:
B() { cout << "B's constructor called" << endl; }
};
class C: public B, public A // Note the order
{
public:
C() { cout << "C's constructor called" << endl; }
};
int main()
{
C c;
return 0;
}
Write a C++ Program that illustrate multi level inheritance.
include
using namespace std;
class Vehicle{
public:
void vehicle(){
cout<<"I am a vehicle";
}
};
class FourWheeler : public Vehicle{
public:
void fourWheeler(){
cout<<"I have four wheels";
}
};
class Car : public FourWheeler{
public:
void car(){
cout<<"I am a car";
}
};
int main(){
Car obj;
obj.car();
obj.fourWheeler();
obj.vehicle();
return 0;
}
4.Write a C++ Program that illustrate Hierarchical inheritance
// C++ program to demonstrate hierarchical inheritance
include
using namespace std;
// base class
class Animal {
public:
void info() {
cout << "I am an animal." << endl;
}
};
// derived class 1
class Dog : public Animal {
public:
void bark() {
cout << "I am a Dog. Woof woof." << endl;
}
};
// derived class 2
class Cat : public Animal {
public:
void meow() {
cout << "I am a Cat. Meow." << endl;
}
};
int main() {
// Create object of Dog class
Dog dog1;
cout << "Dog Class:" << endl;
dog1.info(); // Parent Class function
dog1.bark();
// Create object of Cat class
Cat cat1;
cout << "\nCat Class:" << endl;
cat1.info(); // Parent Class function
cat1.meow();
return 0;
}
Write a C++ program to show inheritance using different levels
include
using namespace std;
// create a base class1
class Base_class
{
// access specifier
public:
// It is a member function
void display()
{
cout << " It is the first function of the Base class " << endl;
}
};
// create a base class2
class Base_class2
{
// access specifier
public:
// It is a member function
void display2()
{
cout << " It is the second function of the Base class " << endl;
}
};
/ create a child_class to inherit features of Base_class and Base_class2 with access specifier. /
class child_class: public Base_class, public Base_class2
{
// access specifier
public:
void display3() // It is a member function of derive class
{
cout << " It is the function of the derived class " << endl;
}
};
int main ()
{
// create an object for derived class
child_class ch;
ch.display(); // call member function of Base_class1
ch.display2(); // call member function of Base_class2
ch.display3(); // call member function of child_class
}
Write a C++ program to write and read object using read and write function.
//C++ program to write and read object using read and write function.
include
include
using namespace std;
//class student to read and write student details
class student
{
private:
char name[30];
int age;
public:
void getData(void)
{ cout<<"Enter name:"; cin.getline(name,30);
cout<<"Enter age:"; cin>>age;
}
ofstream file;
//open file in write mode
file.open("aaa.txt",ios::out);
if(!file)
{
cout<<"Error in creating file.."<<endl;
return 0;
}
cout<<"\nFile created successfully."<<endl;
//write into file
s.getData(); //read from user
file.write((char*)&s,sizeof(s)); //write into file
file.close(); //close the file
cout<<"\nFile saved and closed succesfully."<<endl;
//re open file in input mode and read data
//open file1
ifstream file1;
//again open file in read mode
file1.open("aaa.txt",ios::in);
if(!file1){
cout<<"Error in opening file..";
return 0;
}
//read data from file
file1.read((char*)&s,sizeof(s));
//display data on monitor
s.showData();
//close the file
file1.close();
return 0;
}
Write a C++ program to demonstrate example of tellg() and tellp() function
//C++ program to demonstrate example of tellg() and tellp() function.
include
include
using namespace std;
int main()
{
fstream file;
//open file sample.txt in and Write mode
file.open("sample.txt",ios::out);
if(!file)
{
cout<<"Error in creating file!!!";
return 0;
}
//write A to Z
file<<"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//print the position
cout<<"Current position is: "<<file.tellp()<<endl;
file.close();
//again open file in read mode
file.open("sample.txt",ios::in);
if(!file)
{
cout<<"Error in opening file!!!";
return 0;
}
cout<<"After opening file position is: "<<file.tellg()<<endl;
//read characters untill end of file is not found
char ch;
while(!file.eof())
{
cout<<"At position : "<<file.tellg(); //current position
file>>ch; //read character from file
cout<<" Character \""<<ch<<"\""<<endl;
}
//close the file
file.close();
return 0;
}
Write a C++ Program for Username and Password Registration System.
/ C++ Program for Username and Password Registration System /
include
include
using namespace std;
struct mail
{
char un[50]; // user name
char pd[50]; // passsword
void reg(int);
} obj[5];
void mail::reg(int k)
{
int i=k;
cout<<"\nEnter user name :: ";
cin>>un;
cout<<"\nEnter password :: ";
cin>>pd;
ofstream filout;
filout.open("C:\\Users\\acer\\Documents\\registration.txt",ios::app|ios::binary);
if(!filout)
{
cout<<"\nCannot open file\n";
}
else
{
cout<<"\n";
filout.write((char *)&obj[i],sizeof(mail));
filout.close();
}
cout<<"\n...........You are now registered.......... \n\n";
} // end of sign up or register func
int main()
{
int t;
cout<<"\nEnter Registration Details for User 1 :: \n";
obj[0].reg(0);
cout<<"\nEnter Registration Details for User 2 :: \n";
obj[1].reg(1);
cout<<"\nEnter Registration Details for User 3 :: \n";
obj[2].reg(2);
mail obj2;
ifstream filein;
filein.open("C:\\Users\\acer\\Documents\\registration.txt",ios::in|ios::binary);
if(!filein)
{
cout<<"\nUnable to open file to read\n";
}
else
{
cout<<"\nRegistered Details of All Users :: \n";
filein.read((char *)&obj2,sizeof(obj2));
while(filein)
{
cout<<"\nUsername :: "<<obj2.un<<"\nPasswword :: "<<obj2.pd<<"\n";
filein.read((char *)&obj2,sizeof(obj2));
}
//filein.close();
}
return 0;
}
Write a C++ Program to Maintain House Records using File Handling.
/ C++ Program to Maintain House Records using File Handling /
include
include
include
include
using namespace std;
int opt;
class housing
{
int hno,income;
char name[20],type[20];
public:
void assign()
{
if(income<15000)
strcpy(type,"LIG");
else if(income>=15000)
strcpy(type,"MIG");
else if(income>=25000)
strcpy(type,"HIG");
}
void input()
{
cout<<"\n Enter House Number: ";
cin>>hno;
cout<<"\n House Name: ";
cin>>name;
cout<<"\n Annual Income: ";
cin>>income;
assign();
}
void output()
{
cout<<"House Number: "<<hno<<"\n"<<"House Name: "<<name<<"\n"<<"Annual Income: "<<income<<"\n"<<"Type: "<<type;
}
int retno()
{
return hno;
}
};
int main()
{
housing h,h1;
fstream f;
int hono;
while(true)
{
cout<<"\n 1: Add Record"<<"\n 2: Modify Record"<<"\n 3: Display Records"<<"\n 4: Exit\n"<<endl;
cin>>opt;
if(opt==1)
{
char ch='y';
f.open("C:\\Users\\acer\\Documents\\file4.txt",ios::out|ios::binary|ios::app);
while(ch=='y')
{
cout<<"\n Enter Details: ";
h.input();
f.write((char*)&h,sizeof(h));
cout<<"\n Want to Enter More? y/n: "<<endl;
cin>>ch;
}
f.close();
}
if(opt==2)
{
cout<<"\n Enter House No of Record to be modified: ";
cin>>hono;
f.open("C:\\Users\\acer\\Documents\\file4.txt",ios::in|ios::out|ios::binary|ios::ate);
f.seekg(0);
while(f.read((char*)&h,sizeof(h)))
{
if(h.retno()==hono)
{
cout<<"\n New Value: ";
h1.input();
f.seekp(-sizeof(h),ios::cur);
f.write((char*)&h1,sizeof(h1));
}
}
f.close();
}
if(opt==3)
{
f.open("C:\\Users\\acer\\Documents\\file4.txt",ios::in|ios::binary);
f.seekg(0);
while(f.read((char*)&h,sizeof(h)))
h.output();
f.close();
}
if(opt==4)
exit(0);
cout<<"\nPress any key to continue...... ";
}
return 0;
include<bits/stdc++.h>
using namespace std ;
int main() { char a ; cin >> a ;
cout << "ASCII Value is --> " << (int) a << endl ; return 0 ; }
include<bits/stdc++.h>
using namespace std ;
class temperature { private : float farhen , c ; public : float conversion(float f) { farhen = f ; c = (f-32) * (5.0/9.0) ; return c ; } };
int main() { temperature t ; float f ; cin >> f ; cout << "Temperature in Celsius --> " << t.conversion(f) << endl ; }
// Question - 3
include<bits/stdc++.h>
using namespace std ;
class q2 { private : int t ; public: void swap(int a , int b ) { t = a ; a = b ; b = t ; } } ;
int main() { int a , b ; q2 sa ; cin >> a >> b ; sa.swap( &a , &b ) ; cout << a << " " << b << endl ; }
4.Write a C++ program which explains and show different storage classes auto, extern static and register.
[4:42 pm, 19/11/2022] @#₹_%&-+(): #include
using namespace std;
int staticFunc() { cout << "For Static Variables: "; static int count = 0; count++; return count; }
int nonStaticFunc() { cout << "For Non-Static Variables: ";
}
void registerStorageClass() {
}
int x; void externStorageClass() {
}
void autoStorage() {
}
int main() {
} [4:42 pm, 19/11/2022] @#₹_%&-+(): #include
using namespace std;
int staticFunc() { cout << "For Static Variables: "; static int count = 0; count++; return count; }
int nonStaticFunc() { cout << "For Non-Static Variables: ";
}
void registerStorageClass() {
}
int x; void externStorageClass() {
}
void autoStorage() { cout << "Auto Class;\n" << endl;
}
int main() {
}
include<bits/stdc++.h>
using namespace std ;
int fact(int n) { int facto = 1 ; for (int i = 1 ; i <= n ; i++) facto *= i ; return facto ; }
int main() { int n ; cin >> n ; cout << fact(n) << endl ; }
Write a C++ program to check number is palindrome or not using Function.
include<bits/stdc++.h>
using namespace std ;
define cty cout<<"YES A PALINDROME\n"
define ctn cout<<"NOT A PALINDROME\n"
bool chk(int n) { int og = n ; int rev = 0 ; while(n != 0) { int rem = n % 10 ; rev = rev*10 + rem ; n /= 10 ;
} if (og == rev) return true ; else return false ; }
int main() { int n ; cin >> n ; if (chk(n)) cty ; else ctn ; }
include<bits/stdc++.h>
using namespace std ;
int cube(int n) return nnn ;
int main() { int n ; cin >> n ; cout << cube(n) << endl ; }
// QUESTION --> 4
include<bits/stdc++.h>
using namespace std ;
void swap(int x , int y) { int z = x ; x = y ; y = z ; cout << "Swapped Values " << x << " " << y << endl ; }
int main() { int a , b ; cin >> a >> b ; cout << "Original Values " << a << " " << b << endl ; swap(a,b) ; }
//Question -- 1
include<bits/stdc++.h>
using namespace std ;
int main() { for (int i = 1 ; i < 6 ; i++){ for (int j = 1 ; j <= i ; j++){ cout << i << " " ; } cout << endl ; } return 0 ; }
include<bits/stdc++.h>
using namespace std ;
int main(){ int n ; cin >> n ; for (int i = 0 ; i < 6 ; i++){ for (int j = 0 ; j < 4 ; j++){ if (j == 0 || j == 3 || i == 0 || i == 5) cout << n << " " ; else cout << " " ; } cout << endl ; } }
// Question -- 3
include<bits/stdc++.h>
using namespace std ;
int main(){ time_t timetoday ; time (&timetoday) ; cout << "Current Date and Time --> " << asctime(localtime(&timetoday)) << endl ; return 0 ; }
// Question -- 4
include<bits/stdc++.h>
using namespace std ;
int main(){ int p , r , t ; cout << "Enter Principal --> " ; cin >> p ; cout << "Enter Rate of Interest --> " ; cin >> r ; cout << "Enter Time Period --> " ; cin >> t ; int si = prt/100 ; cout << "Simple Interest Calculated is --> " << si << endl ; return 0 ;
}
// Question - 1
include
using namespace std ;
class cls{ private: int n ; public: void get(int x) { n = x ; } void prt() { cout << "The Value of n is -> " << n << endl ; } void operator - (void) { n = -n ; }
};
int main(){ cls op ; op.get(69); -op ; op.prt() ; return 0 ; }
// Question - 2
include
using namespace std ;
class cls{ private: int n ; public: void get(int x) { n = x ; } void prt() { cout << n << endl ; } void operator-- (void) // unary decrement overloading { n = --n ; }
void operator++ (void) // unary increment overloading { n = ++n ; } };
int main(){ cls op ; op.get(69);
}
// Question - 3
include
using namespace std ;
class cls{ private: int n ; public: void get(int x) { n = x ; } void prt() { cout << n << endl ; } void operator! (void) // unary ! overloading { n = !n ; }
};
int main(){ cls op ; op.get(69); cout << "Before ! Overloading --> " ; op.prt() ; !op ; cout << "After ! Overloading --> " ; op.prt() ; return 0 ; }
include
using namespace std;
class date { private: int dd, mm, yy;
}; int main () { date date1; date1.display ();
return 0; }
include
using namespace std;
class stu { private: char name[20],add[20]; int roll,zip;
};
stu :: stu() { cout<<"\nThis is Student Details constructor called..........."<<endl; }
void stu :: read() { cout<<"\nEnter the student Name :: "; cin>>name; cout<<"\nEnter the student roll no :: "; cin>>roll; cout<<"\nEnter the student address :: "; cin>>add; cout<<"\nEnter the Zipcode :: "; cin>>zip; }
void stu :: disp() { cout<<"\nThe Entered Student Details are shown below ::---------- \n"; cout<<"\nStudent Name :: "<<name<<endl; cout<<"\nRoll no is :: "<<roll<<endl; cout<<"\nAddress is :: "<<add<<endl; cout<<"\nZipcode is :: "<<zip; }
stu :: ~stu() { cout<<"\n\nStudent Detail is Closed.............\n"; }
int main() { stu s; s.read (); s.disp ();
}
include
using namespace std; class ParamA { private: int b, c; public: ParamA (int b1, int c1) { b = b1; c = c1; } int getX () { return b; } int getY () { return c; } }; int main () { ParamA p1(10, 15); cout << "p1.b = " << p1. getX() << ", p1.c = " << p1.getY(); return 0; }
include
using namespace std;
class box { double length,width,height; double volume;
};
box::box(double a,double b,double c) { length=a; width=b; height=c; volume=lengthwidthheight; }
void box::vol() { cout<<"\nDimensions of Box are :: \n"; cout<<"\nLength of Box :: "<<length<<"\n"; cout<<"\nWidth of Box :: "<<width<<"\n"; cout<<"\nHeight of Box :: "<<height<<"\n"; cout<<"\nVolume of Box :: "<<volume<<"\n"; }
int main() { box x(2.4,5.7,2.1),y(3.3,4.4,5.5);
}
/ C++ program to read and print employee information with department and pf information using hierarchical inheritance. /
include
include
using namespace std;
//Base Class - basicInfo class basicInfo { protected: char name[30]; int empId; char gender;
public: void getBasicInfo(void) { cout << "Enter Name: "; cin.ignore(1); cin.getline(name, 30); cout << "Enter Emp. Id: "; cin >> empId; cout << "Enter Gender: "; cin >> gender; } };
//Base Class - deptInfo class deptInfo : private basicInfo { protected: char deptName[30]; char assignedWork[30]; int time2complete;
public: void getDeptInfo(void) { getBasicInfo(); //to get basic info of an employee cout << "Enter Department Name: "; cin.ignore(1); cin.getline(deptName, 30); cout << "Enter assigned work: "; fflush(stdin); cin.getline(assignedWork, 30); cout << "Enter time in hours to complete work: "; cin >> time2complete; } void printDeptInfo(void) { cout << "Employee's Information is: " << endl; cout << "Basic Information...:" << endl; cout << "Name: " << name << endl; //accessing protected data cout << "Employee ID: " << empId << endl; //accessing protected data cout << "Gender: " << gender << endl << endl; //accessing protected data
};
//another Base Class : loadInfo class loanInfo : private basicInfo { protected: char loanDetails[30]; int loanAmount;
public: void getLoanInfo(void) { getBasicInfo(); //to get basic info of an employee cout << "Enter Loan Details: "; cin.ignore(1); cin.getline(loanDetails, 30); cout << "Enter loan amount: "; cin >> loanAmount; } void printLoanInfo(void) { cout << "Employee's Information is: " << endl; cout << "Basic Information...:" << endl; cout << "Name: " << name << endl; //accessing protected data cout << "Employee ID: " << empId << endl; //accessing protected data cout << "Gender: " << gender << endl << endl; //accessing protected data
};
int main() { //read and print department information deptInfo objD;
}
include
using namespace std;
// Base class class A { public: A() { cout << "Base class A constructor \n"; } };
// Derived class B class B: public A { public: B() { cout << "Class B constructor \n"; } };
// Derived class C class C: public B { public: C() { cout << "Class C constructor \n";
};
// Driver code int main() { C obj; return 0; }
include
using namespace std;
class Base { public: void print() { cout << "Base Function" << endl; } };
class Derived : public Base { public: void print() { cout << "Derived Function" << endl; } };
int main() { Derived derived1; derived1.print(); return 0; }
/ C++ Program to enter student details using Virtual Class /
include
using namespace std;
class student
{ protected:
};
class test: virtual public student { protected: float sub_marks;
};
class sports: public virtual student { protected: float sp_marks;
};
class result: public test, public sports {
};
int main() { result R; R.get_no(34); R.get_submarks(67.2); R.get_spmarks(98.9); R.put_result();
}
include
using namespace std;
class Account {
public:
float salary = 60000;
};
class Programmer: public Account {
public:
float bonus = 5000;
};
int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
include
using namespace std;
class A { public: A() { cout << "A's constructor called" << endl; } };
class B { public: B() { cout << "B's constructor called" << endl; } };
class C: public B, public A // Note the order { public: C() { cout << "C's constructor called" << endl; } };
int main() { C c; return 0; }
include
using namespace std;
class Vehicle{ public: void vehicle(){ cout<<"I am a vehicle"; } };
class FourWheeler : public Vehicle{ public: void fourWheeler(){ cout<<"I have four wheels"; } };
class Car : public FourWheeler{ public: void car(){ cout<<"I am a car"; } }; int main(){ Car obj; obj.car(); obj.fourWheeler(); obj.vehicle(); return 0; }
4.Write a C++ Program that illustrate Hierarchical inheritance
// C++ program to demonstrate hierarchical inheritance
include
using namespace std;
// base class class Animal { public: void info() { cout << "I am an animal." << endl; } };
// derived class 1 class Dog : public Animal { public: void bark() { cout << "I am a Dog. Woof woof." << endl; } };
// derived class 2 class Cat : public Animal { public: void meow() { cout << "I am a Cat. Meow." << endl; } };
int main() { // Create object of Dog class Dog dog1; cout << "Dog Class:" << endl; dog1.info(); // Parent Class function dog1.bark();
}
include
using namespace std;
// create a base class1
class Base_class
{
// access specifier
public:
// It is a member function
void display()
{
cout << " It is the first function of the Base class " << endl;
}
};
// create a base class2
class Base_class2
{
// access specifier
public:
// It is a member function
void display2()
{
cout << " It is the second function of the Base class " << endl;
}
};
/ create a child_class to inherit features of Base_class and Base_class2 with access specifier. /
class child_class: public Base_class, public Base_class2
{
};
int main ()
{
// create an object for derived class
child_class ch;
ch.display(); // call member function of Base_class1
ch.display2(); // call member function of Base_class2
ch.display3(); // call member function of child_class
}
//C++ program to write and read object using read and write function.
include
include
using namespace std;
//class student to read and write student details class student { private: char name[30]; int age; public: void getData(void) { cout<<"Enter name:"; cin.getline(name,30); cout<<"Enter age:"; cin>>age; }
};
int main() { student s;
}
//C++ program to demonstrate example of tellg() and tellp() function.
include
include
using namespace std;
int main() { fstream file; //open file sample.txt in and Write mode file.open("sample.txt",ios::out); if(!file) { cout<<"Error in creating file!!!"; return 0; } //write A to Z file<<"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //print the position cout<<"Current position is: "<<file.tellp()<<endl; file.close();
}
/ C++ Program for Username and Password Registration System /
include
include
using namespace std;
struct mail { char un[50]; // user name char pd[50]; // passsword void reg(int); } obj[5];
void mail::reg(int k) { int i=k; cout<<"\nEnter user name :: "; cin>>un; cout<<"\nEnter password :: "; cin>>pd;
} // end of sign up or register func
int main() { int t; cout<<"\nEnter Registration Details for User 1 :: \n"; obj[0].reg(0); cout<<"\nEnter Registration Details for User 2 :: \n"; obj[1].reg(1); cout<<"\nEnter Registration Details for User 3 :: \n"; obj[2].reg(2);
}
/ C++ Program to Maintain House Records using File Handling /
include
include
include
include
using namespace std;
int opt;
class housing { int hno,income; char name[20],type[20]; public: void assign() { if(income<15000) strcpy(type,"LIG"); else if(income>=15000) strcpy(type,"MIG"); else if(income>=25000) strcpy(type,"HIG"); }
};
int main() { housing h,h1; fstream f; int hono;
}