0% found this document useful (0 votes)
12 views23 pages

Varsha Madam

The document contains multiple C++ class designs for managing and displaying information about employees, fruits, students, cars, and complex numbers. It includes methods for reading input, displaying data, and performing calculations such as factorial, palindrome checks, and matrix addition. Each class demonstrates various concepts of object-oriented programming including constructors, friend functions, and private methods.

Uploaded by

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

Varsha Madam

The document contains multiple C++ class designs for managing and displaying information about employees, fruits, students, cars, and complex numbers. It includes methods for reading input, displaying data, and performing calculations such as factorial, palindrome checks, and matrix addition. Each class demonstrates various concepts of object-oriented programming including constructors, friend functions, and private methods.

Uploaded by

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

● 1] Design an employee class for reading and displaying the

employee information.

Input:

#include <iostream>

using namespace std;


class employee

{ int age;
char name[20];
float sal; void
getInfo()
{
cout<<"Enter your name ,age and salary :=";
cin>>name>>age>>sal;
}
public:
void displayInfo()
{ getInfo(); cout<<"\n your name ,age and salary are"<<name<<"\
n"<<age<<"\n"<<sal;

}
};
int main()
{
employee e;
e.displayInfo();
return 0;
}
output:
SIDDHESH MALI 2510
FYIT[B]

● Design an Fruit class for reading and displaying the


Fruit information.

Input:

#include <iostream>

using namespace std;


class Fruit
{
int num; char
name[20]; float
price; void
getInfo()
{

cout<<"Enter number of fruit,name of fruit,price of fruit:";


cin>>num>>name>>price;

} public: void
displayInfo()
{
getInfo(); cout<<"Enter number of fruit,name of
fruit,price of fruit
are:"<<"\n"<<num<<"\n"<<name<<"\n"<<price;
}
};
int main()
{
Fruit F;
F.displayInfo();

return 0;
}

Output:

● 2] Design the class student containing getData() and displayData() as


two of its methods which will be used for reading and displaying the
student information respectively where getData() will be a private
method.

#include <iostream>

using namespace std;


class Student
{ int roll;
char name[20];
float per;
void getInfo()
{

cout<<"Enter Roll no,name,percentage:";


cin>>roll>>name>>per;
SIDDHESH MALI 2510
FYIT[B]
}
public:
void displayInfo()
{ getInfo(); cout<<"Enter Roll no,name,percentage:"<<"\n"<<roll<<"\
n"<<name<<"\n"<<per;
}
};
int main()
{

Student s;
s.displayInfo();

return 0;
}

Output:

● Design the class student containing getData() and displayData() as


two of its methods which will be used for reading and displaying
the student information respectively where getData() will be a
private method.

Input:
#include <iostream>

using namespace std;


class Student
{
int roll;
char name[20];
float per;
public:
void getInfo()
{

cout<<"Enter Roll no,name,percentage:";


cin>>roll>>name>>per;

void displayInfo()
{
//getInfo(); cout<<"Enter Roll no,name,percentage:"<<"\n"<<roll<<"\
n"<<name<<"\n"<<per;
}
};
int main()
{

Student s;
s.getInfo();
s.displayInfo();

return 0;

Output:
SIDDHESH MALI 2510
FYIT[B]
#include <iostream>

using namespace std;

class cars
{ int price,c,m;
char brand[20];
void accept()
{

cout<<"price,chassis number,model no,brand name,except:";


cin>>price>>c>>m>>brand;

} public: void
displayInfo()
{
accept(); cout<<"Enter price,chassis number,model
no,brand
name,except:"<<"\n"<<price<<"\n"<<c<<"\n"<<m<<"\n"<<brand<<"\n";
}
};

int main()
{
cars c1;
c1.displayInfo();
cars c2;
c2.displayInfo();

return 0;
}
SIDDHESH MALI 2510
FYIT[B]
3] Design the class Demo which will contain the following
methods: readNo(), factorial() for calculating the factorial of a
number, reverseNo() will reverse the given number,
isPalindrome() will check the given number is palindrome,
isArmstrong() which will calculate the given number is
AarmStrong or not.Where readNo() will be a private method.

IINPUT:
#include<iostream>
#include<math.h>
using namespace std;

class Demo
{
int num, len=0;

public:
Demo()
{
readNo();
}

void factorial()
{
int a = num;
int i = 1;
while(a>0)
{ i = i * a; a--;
len++;
}
cout<<"Factorial of "<<num<<" is :"<<i<<"\n";
}

void reverse()
{
int a = num;
int rev = 0;
while(a>0)
{
rev = rev*10 + a%10;
a /= 10;
}
cout<<"Reverse of "<<num<<" is : "<<rev<<"\n";
}

void isPalindrome()
{
int a = num;
int rev = 0;
while(a>0)
{
rev =rev*10 + a%10;
a /= 10;
}
if(rev == num)
{
cout<<num <<"is a Palindrome number \n";
}
else
{
cout<<num<<" is NOT a Palindrome number \n";
}
}

void isArmstrong()
{

int n,rem, rsl=0 ;


n=num;
while(n!=0)
{
rem = n % 10; rsl +=
rem * rem * rem; n /=
10;
} if(rsl ==
num)
{
cout<<num <<" is an Armstrong number \n";
}
else
{
cout <<num <<" is NOT an Armstrong number \n";
}
}
private:

void readNo()
SIDDHESH MALI 2510
FYIT[B]
{
cout<<"Enter any number : ";
cin>>num;
}
};

int main()
{
Demo d = Demo();
d.factorial();
d.reverse();
d.isPalindrome();
d.isArmstrong();
return 0;
}

OUTPUT:

Default constructor:
INPUT:
#include <iostream>
#include <conio.h>
using namespace std;
class construct1
{ int a,b; public:
construct1()
{
cout<<"\n Enter two number:";
cin>>a>>b; cout<<"\n A
="<<a<<"\t B ="<<b;
}

};

int main()
{
construct1 c; construct1
cc=construct1(); return 0;
}
OUTPUT:

Parameterised constructor:
INPUT:
#include <iostream>
#include <conio.h>
using namespace std;
class demo
{ int x; public:
demo(int n)
{
x=n;
} void
display()
{
cout<<"\n value of X:"<<x;

};

int main()
{
SIDDHESH MALI 2510
FYIT[B]
demo d(50) ;
d.display();
return 0;
}
OUTPUT:

Copy constructor:
INPUT:
#include <iostream>
#include <conio.h>
using namespace std;
class construct1
{ int a,b; public:
construct1()
{
a=5;
b=10;
} construct1(int n,int
m)
{
a=n;
b=m;
}
construct1(construct1 &cp)
{
a=cp.a;
b=cp.b;
}
void show()
{

cout<<"\n A ="<<a<<"\t B ="<<b;


}

};

int main()
{
construct1 c(10,5);
construct1 cc(c); cout<<"\n
object 1 Data :"; c.show();
cout<<"\n \n object 2 Data :";
cc.show();

return 0;
}
SIDDHESH MALI 2510
FYIT[B]
FYIT[B]
OUTPUT:
SIDDHESH MALI 2510
FRIEND FUNCTION:

INPUT:
#include <iostream>

using namespace std;


class EgFriend
{

int n,m;
public:
friend void display(EgFriend);
void getdata()
{
cout<<"Enter two numbers :";
cin>>n>>m;
}
void show()
{
cout<<"\n N="<<n; cout<<"\
n M="<<m;

};
int main()
{
EgFriend ef; ef.getdata(); cout<<" \n object Data Display
through member function"; ef.show(); cout<<" \n object
Data Display through Friend function"; display(ef);

}
void display( EgFriend e)
{
cout<<"\n N ="<<e.n;
cout<<"\n M="<<e.m;
}
OUTPUT:
SIDDHESH MALI 2510
FYIT[B]
FYIT[B]
SIDDHESH MALI 2510
Design a class complex for adding two complex numbers and also show the use
of constructor:

#include <iostream>

using namespace std;


class complex
{ float a,b;
public:
complex()
{
a=b=0;
} complex(float x,float
y)
{
x=a;
y=b;
}
void display()
{
cout<<x<<"+"<<y<<"i";
}
complex add(complex c)
{
complex tmp;
tmp.x=x+c.x;
tmp.y=y+c.y;
return tmp;
}
};
int main()
{
complex c1(4,5),c2(1.25,6),c3;
c3=c1.add(c2);
cout&lt;&lt;&quot;c1=&quot;
c1.display();
SIDDHESH MALI 2510
FYIT[B]
cout<<"\n";
cout<<"c2=";
c2.display(); cout<<"\
n"; cout<<"c1+c2=";
c3.display();
return 0;
}

Friend function for adding two distances :

#include<iostream> using namespace

std; class Complex

int num; public:

void setNum(int d)

num=d;

friend int add(Complex,Complex);

};
SIDDHESH MALI 2510
FYIT[B]
int add(Complex c1,Complex c2)

int sum=0;

sum=c1.num+c2.num;

return sum;

int main()
{

Complex c1,c2; c1.setNum(10);

c2.setNum(25);

cout<<"Distance: "<<add(c1,c2);

return 0;

Friend function for adding two matrix:

#include<iostream> using

namespace std;
SIDDHESH MALI 2510
FYIT[B]
class mat2;

class mat1

{ int a[5][5],i,j; public: void

get1(); friend void

add(mat1,mat2);

};

void mat1::get1()

cout<<"\n Matrix1 \n"; cout<<"\n Enter

element for a 3*3 matrix1 \n"; for(i=0;i<3;i++)

{ for(j=0;j<3;j+

+)

{ cin>>a[i]

[j]; }

class mat2

{ int b[5][5],i,j; public: void

get2(); friend void

add(mat1,mat2);

};
SIDDHESH MALI 2510
FYIT[B]
void mat2::get2()

cout<<"\n Matrix 2 \n"; cout<<"\n Enter

element for a 3*3 matrix 2 \n"; for(i=0;i<3;i++)

{ for(j=0;j<3;j+

+)

{ cin>>b[i]

[j];

}
void add(mat1 m,mat2 n)

{ int c[5]

[5],i,j;

cout<<"\n Output \n"; cout<<"Addition

of two matrices is:- \n"; for(i=0;i<3;i++)

{ for(j=0;j<3;j+

+)

{ c[i][j]=m.a[i][j]+n.b[i]

[j];

}
SIDDHESH MALI 2510
FYIT[B]
} for(i=0;i<3;i+

+)

{ for(j=0;j<3;j+

+)

{ cout<<c[i][j]<<"

";

} int

main()

{
mat1 m;

mat2 n;

m.get1();

n.get2();

add(m,n);

return 0;

}
SIDDHESH MALI 2510
FYIT[B]

You might also like