Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

1. Write a program to check leap years.

#include<iostream.h>
#include<conio.h>

void leap(int);
int main()
{
int l;
clrscr();
cout<<"Enter any year:";
cin>>l;
leap(l);
getch();
return 0;
}
void leap(int e)
{
if(e%4==0)
{
cout<<"The entered year is a leap year.";
}
else
{
cout<<"The entered year is not a leap year.";
}
getch();
}

2. Write a program for increment ++ and decrement


-- operator overloading.
#include<iostream.h>
#include<conio.h>

int main()
{
clrscr();
cout<<"1+2="<<1+2<<endl;
cout<<"'1'+'2'="<<'1'+'2';
getch();
return 0;
}

4. Write a program to create a class called employee


whose member data are : emp_code, emp_name,
age, salary. Read and display employee information
whose age is more than 40 and salary is more than
5000.
#include<iostream.h>
#include<conio.h>

class employee
{
int emp_code,age,salary;
char emp_name[30];

public:
void getdata();
void putdata();
};
void employee::getdata()
{
cout<<"Enter employee code:";
cin>>emp_code;
cout<<"Enter employee name:";
cin>>emp_name;
cout<<"Enter employee age:";
cin>>age;
cout<<"Enter employee salary:";
cin>>salary;
}
void employee :: putdata()
{
if(age>40 && salary>5000)
{
cout<<"Employee code:"<<emp_code;
cout<<"\nEmployee name:"<<emp_name;
cout<<"\nEmployee age:"<<age;
cout<<"\nEmployee salary:"<<salary;
}
}
int main()
{
employee emp[3];
int i;
clrscr();
for(i=0;i<3;i++)
{
cout<<"*****Details of Employee"<<i+1<<"*****"<<endl;
emp[i].getdata();
}
cout<<"***Employees who's age and salary is more than 40 and 5000 respectively:***"<<endl;
for(i=0;i<3;i++)
{
emp[i].putdata();
}
getch();
return 0;
}

5. Write a program to find the area of various


geometrical shapes by function overloading.
#include<iostream.h>
#include<conio.h>

int area(int);
int area(int,int);
float area(float);
float area(float,float);

int main()
{
int s,l,b;
float r,bs,ht;
clrscr();
cout<<"Enter side of square:";
cin>>s;
cout<<"Enter length and breadth of rectangle:";
cin>>l>>b;
cout<<"Enter radius of circle:";
cin>>r;
cout<<"Enter base and height of triangle:";
cin>>bs>>ht;
cout<<"Area of circle:"<<area(s);
cout<<"\nArea of rectangle:"<<area(l,b);
cout<<"\narea of circle:"<<area(r);
cout<<"\nArea of triangle:"<<area(bs,ht);
getch();
return 0;
}
int area(int s)
{
return(s*s);
}
int area(int l, int b)
{
return(l*b);
}
float area(float r)
{
return(3.14*r*r);
}
float area(float bs,float ht)
{
return(0.5*bs*ht);
}

6. Write a program for static data members.


#include <iostream.h>
#include <string.h>
class Student {
private:

int rollNo;
char name[10];
int marks;

public:

static int objectCount;


Student() { objectCount++;
}
void getdata();
void putdata();
void getdata() {
cout << "Enter roll number: "<<endl;
cin >> rollNo;
cout << "Enter name: "<<endl;
cin >> name;
cout << "Enter marks: "<<endl;
cin >> marks; }
void putdata() {
cout<<"Roll Number = "<< rollNo <<endl;
cout<<"Name = "<< name <<endl;
cout<<"Marks = "<< marks <<endl;
cout<<endl; }
};

int Student::objectCount = 0;
int main(void) {
Student s1;
s1.getdata();
s1.putdata();
Student s2;
s2.getdata();
s2.putdata();
Student s3;
s3.getdata();
s3.putdata();
cout << "Total objects created = " <<
Student::objectCount << endl;
return 0;
}

7. Write a program for static member functions.


#include<iostream.h>
#include<conio.h>
class Student
{
private:

int rollNo;
char name[10];
int marks;
static int objectCount;
public:

void getdata();
void putdata();
static void show_count()
{
cout<<"No.ofstudents: "<< objectCount++<<endl;
}

};
void Student :: getdata() {
cout << "Enter roll number: "<<endl;
cin >> rollNo;
cout << "Enter name: "<<endl;
cin >> name;
cout << "Enter marks: "<<endl;
cin >> marks; }
void Student :: putdata() {
cout<<"Roll Number = "<< rollNo <<endl;
cout<<"Name = "<< name <<endl;
cout<<"Marks = "<< marks <<endl;
cout<<endl; }
int Student::objectCount = 0;
int main()
{

clrscr();
Student::show_count();
Student::show_count();
getch();
return 0;

8. Write a program to generate an Electricity bill


using class.
#include<iostream>
#include<conio.h>

class e_bill
{
private:
int c_no;
char c_name[20];
int units;
double bill;
public:
void get()
{
cout<<"Enter Details of Customer Below :: \n" <<endl;
cout<<"Enter Customer No. :: ";
cin>>c_no;
cout<<"\nEnter Customer Name :: ";
cin>>c_name;
cout<<"\nEnter No. of Units used :: ";
cin>>units;
}

void put()
{
cout<<"\nEntered Details of Customer are :: " <<endl;
cout<<"\nCustomer No. is : "<<c_no;
cout<<"\n\nCustomer Name is : "<<c_name;
cout<<"\n\nNumber of Units Consumed : "<<units;
cout<<"\n\nBill of Customer : "<<bill;
}

void calc_bill()
{
if(units<=100)
bill=units*1.20;
else if(units<=300)
bill=100*1.20+(units-100)*2;
else
bill=100*1.20+200*2+(units-300)*3;
}
};

int main()
{
e_bill b1;
b1.get();
b1.calc_bill();
b1.put();
cout<<"\n";

return 0;
}

9. Write a program to find square root of float and


integer using inline function.
#include<iostream.h>
#include<conio.h>

inline float square(float);


inline float square(float n)
{
return(n*n);
}
int main()
{
float n;
clrscr();
cout<<"Enter any number:";
cin>>n;
cout<<"Square root:"<<square(n);
getch();
return 0;
}

10.Write a program to calculate the sum of 3


numbers using Default arguments.
#include<iostream.h>
#include<conio.h>

int sum (int a,int b=10,int c=20);

int main()
{

clrscr();
cout<<sum(1)<<endl;
cout<<sum(1,2)<<endl;
cout<<sum(1,2,3)<<endl;
getch();
return 0;
}
int sum(int a, int b, int c)
{
return(a+b+c);
}

11.Write a program for default constructor.


#include<iostream.h>
#include<conio.h>

class a
{
int n1,n2;
public:
a()
{
n1=22;
n2=25;
}
void putdata()
{
cout<<"n1="<<n1<<endl;
cout<<"n2="<<n2<<endl;
}
};
int main()
{
clrscr();
a obj;
obj.putdata();
getch();
return 0;
}

12.Write a program for parameterized constructor.


#include<iostream.h>
#include<conio.h>

class a
{
int num1,num2;
public:
a(int n1, int n2)
{
num1=n1;
num2=n2;
}
void display()
{
cout<<"Num1:"<<num1<<endl;
cout<<"Num2:"<<num2<<endl;
}
};
int main()
{
clrscr();
a obj(3,8);
obj.display();
getch();
return 0;
}

13.Write a program to generate a student's mark


sheet using class.
#include<iostream.h>
#include<conio.h>

class marksheet
{
int roll_no,m1,m2,m3,total,per;
char name[30],grade;

public:
void getdata();
};
void marksheet::getdata()
{
cout<<"Enter student roll no:";
cin>>roll_no;
cout<<"Enter student name:";
cin>>name;
cout<<"Enter student marks 1:";
cin>>m1;
cout<<"Enter student marks 2:";
cin>>m2;
cout<<"Enter student marks 3:";
cin>>m3;
total=m1+m2+m3;
per=(total)/3;
cout<<"Percentage:"<<per;
}
int main()
{
int i;
clrscr();
marksheet student[3];
for(i=0;i<3;i++)
{
cout<<"\n*****Details of student***** "<<i+1<<endl;
student[i].getdata();
}
getch();
return 0;
}

14.Write a program to calculate the volume of a


cuboid using an inline function.
#include<iostream.h>
#include<conio.h>

inline int cuboid(int,int,int);


inline int cuboid(int l,int b,int h)
{
return(l*b*h);
}
int main()
{
int l,b,h;
clrscr();
cout<<"Enterlength breath and height of cuboid:";
cin>>l>>b>>h;
cout<<"volume of cuboid is:"<<cuboid(l,b,h);
getch();
return 0;
}

You might also like