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

C++problem Sheet

The document contains 14 code snippets demonstrating various C++ concepts like classes, objects, functions, constructors, inheritance etc. Some key concepts covered include: defining classes with member functions and data, using constructors, function overloading, static class members, inline functions, default arguments, generating marksheets and bills using classes.

Uploaded by

Driti Gabani
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)
53 views

C++problem Sheet

The document contains 14 code snippets demonstrating various C++ concepts like classes, objects, functions, constructors, inheritance etc. Some key concepts covered include: defining classes with member functions and data, using constructors, function overloading, static class members, inline functions, default arguments, generating marksheets and bills using classes.

Uploaded by

Driti Gabani
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/ 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;

0
leap(l);
getch();
return 0;
}
void leap(int e)
{
if(e%4==0)
{

}
09
cout<<"The entered year is a leap year.";
12
else
{
cout<<"The entered year is not a leap year.";
}
getch();
}
22

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>

0
#include<conio.h>

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

public:
void getdata();
void putdata();
09
12
};
void employee::getdata()
{
cout<<"Enter employee code:";
cin>>emp_code;
cout<<"Enter employee name:";
22

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++)

0
{
emp[i].putdata();
}

}
getch();
return 0;
09
5. Write a program to find the area of various
12
geometrical shapes by function overloading.
#include<iostream.h>
#include<conio.h>

int area(int);
int area(int,int);
22

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)

0
{
return(l*b);
}
float area(float r)
{

}
return(3.14*r*r);

float area(float bs,float ht)


{
return(0.5*bs*ht);
09
12
}

6. Write a program for static data members.


#include <iostream.h>
22

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

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

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;

0
cin >> name;
cout << "Enter marks: "<<endl;
cin >> marks; }
void Student :: putdata() {

09
cout<<"Roll Number = "<< rollNo <<endl;
cout<<"Name = "<< name <<endl;
cout<<"Marks = "<< marks <<endl;
cout<<endl; }
int Student::objectCount = 0;
int main()
12
{

clrscr();
Student::show_count();
Student::show_count();
getch();
22

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 :: ";

0
cin>>units;
}

void put()
{
09
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;
12
}

void calc_bill()
{
if(units<=100)
bill=units*1.20;
22

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

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

10.Write a program to calculate the sum of 3


22

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>

0
class a
{
int n1,n2;
public:
a()
{

}
n1=22;
n2=25;
09
12
void putdata()
{
cout<<"n1="<<n1<<endl;
cout<<"n2="<<n2<<endl;
}
};
22

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;

0
cout<<"Num2:"<<num2<<endl;
}
};
int main()
{
clrscr();
a obj(3,8);
obj.display();
getch();
return 0;
09
12
}

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


sheet using class.
22

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

0
int main()
{
int i;
clrscr();
marksheet student[3];
for(i=0;i<3;i++)
{

}
09
cout<<"\n*****Details of student***** "<<i+1<<endl;
student[i].getdata();
12
getch();
return 0;
}

14.Write a program to calculate the volume of a


22

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

0
09
12
22

You might also like