#include <iostream>
using namespace std;
class Shape
{
protected:
int A;
public:
Shape()
{
A=0;
}
void show();
};
class Triangle:public Shape
{
int h, b;
public:
void Geth();
void Getb();
void Showhb();
void CalTriA();
};
class Rectangle:public Shape
{
int l, b;
public:
void Getl();
void Getb();
void Showlb();
void CalRecA();
};
void Shape::show()
{
cout << endl << "Area=" << A << endl ;
}
void Triangle::Geth()
{
cout << endl << "Enter height of the triangle:" ;
cin >> h ;
}
void Triangle::Getb()
{
cout << endl << "Enter base of the triangle:" ;
cin >> b ;
}
void Triangle::Showhb()
{
cout << endl << "Triangle:" << endl ;
cout << "Height=" << h << endl << "Base=" << b << endl ;
}
void Triangle::CalTriA()
{
A = (h*b)/2;
}
void Rectangle::Getl()
{
cout << endl << "Enter length of Rectangle:" ;
cin >> l ;
}
void Rectangle::Getb()
{
cout << endl << "Enter breadth of Rectangle:" ;
cin >> b ;
}
void Rectangle::Showlb()
{
cout << endl << "Rectangle" << endl ;
cout << "Length=" << l << endl << "Breadth=" << b << endl ;
}
void Rectangle::CalRecA()
{
A = l*b ;
}
int main()
{
Triangle T;
T.Geth();
T.Getb();
Rectangle R;
R.Getl();
R.Getb();
T.Showhb();
T.CalTriA();
T.show();
R.Showlb();
R.CalRecA();
R.show();
return 0;
}
C and C++ programs, games, softwares. Easy programming guide. Learn to code and enjoy coding in CoderNepal
Home » Object Oriented Programming
Showing posts with label Object Oriented Programming. Show all posts
Showing posts with label Object Oriented Programming. Show all posts
Tuesday, April 18, 2017
C++ code showing Hierarchical Inheritance to calculate Area
This is the solution for T.U.2068 Q.N.3
C++ code showing Inheritance and Data Conversion
This is the solution for T.U. 2067 Q.N.3
#include <iostream>
using namespace std;
class clock
{
protected:
int hr, min, sec ;
public:
clock()
{
hr = min = sec = 0;
}
clock(int x)
{
hr = min = sec = x ;
}
void Show();
};
class wall_clock:public clock
{
int TotalTime;
public:
wall_clock():clock()
{
TotalTime = 0 ;
}
wall_clock(int x):clock(x)
{
}
void Add(int, int, int);
void ShowTime();
};
void clock::Show()
{
cout << endl << "Initially" << endl ;
cout << endl << hr << ":" << min << ":" << sec << endl ;
}
void wall_clock::Add(int a, int b, int c)
{
hr+=a;
min+=b;
sec+=c;
}
void wall_clock::ShowTime()
{
if(sec>60)
{
min += sec/60;
sec = sec%60;
}
if(min>60)
{
hr += min/60 ;
min = min%60;
}
cout << endl << hr << ":" << min << ":" << sec << endl ;
}
int main()
{
wall_clock WC1,WC2;
WC1 = 0 ;
WC2 = 0 ;
WC1.Show();
WC2.Show();
WC1.Add(1,17,65);
WC2.Add(4,45,49);
WC1.ShowTime();
WC2.ShowTime();
return 0;
}
C++ code showing Hierarchical Inheritance
This is the solution for T.U. 2066 Q.N. 3
#include <iostream>
using namespace std;
class Student
{
protected:
float avg;
public:
void Show();
};
class ComputerScience:public Student
{
float OOPL, OS, NM ;
public:
void GetMarks1();
void ShowMarks1();
void CalAvg1();
};
class Mathematics:public Student
{
float Calculus, LA, Geometry ;
public:
void GetMarks2();
void ShowMarks2();
void CalAvg2();
};
void Student::Show()
{
cout << endl << "Average=" << avg << endl ;
}
void ComputerScience::GetMarks1()
{
cout << endl << "Enter marks of:" << endl ;
cout << endl << "OOPL:" ;
cin >> OOPL ;
cout << endl << "NM:" ;
cin >> NM ;
cout << endl << "OS:" ;
cin >> OS ;
}
void ComputerScience::ShowMarks1()
{
cout << endl << "Computer Science:" << endl ;
cout << endl << "OOPL=" << OOPL << endl << "NM=" << NM << endl << "OS=" << OS << endl ;
}
void ComputerScience::CalAvg1()
{
avg = (OOPL + NM + OS)/3 ;
}
void Mathematics::GetMarks2()
{
cout << endl << "Enter Marks of:" << endl ;
cout << endl << "Calculus:" ;
cin >> Calculus ;
cout << endl << "LA:" ;
cin >> LA ;
cout << endl << "Geometry:" ;
cin >> Geometry ;
}
void Mathematics::ShowMarks2()
{
cout << endl << "Mathematics:" << endl ;
cout << endl << "Calculus=" << Calculus << endl << "LA=" << LA << endl << "Geometry=" <<
Geometry << endl ;
}
void Mathematics::CalAvg2()
{
avg = (Calculus + LA + Geometry)/3 ;
}
int main()
{
ComputerScience CS;
CS.GetMarks1();
CS.CalAvg1();
Mathematics M;
M.GetMarks2();
M.CalAvg2();
CS.ShowMarks1();
CS.Show();
M.ShowMarks2();
M.Show();
return 0;
}
Monday, April 17, 2017
C++ code for Unary Operator Overloading(--) using Friend Function
This is the solution that increases an integer value by 1 by overloaded operator using Friend Function (T.U. 2072)
#include <iostream>
using namespace std;
class Decrement
{
int a;
public:
Decrement()
{
a=10;
}
Decrement(int x)
{
a=x;
}
friend operator++(Decrement &I,int); //Use & while using Friend Function
friend operator++(Decrement &I);
void print();
};
operator++(Decrement &I,int)
{
I.a--;
}
operator++(Decrement &I)
{
--I.a;
}
void Decrement::print()
{
cout << endl << "a=" << a << endl ;
}
int main()
{
Decrement D1;
D1.print();
D1--;
D1.print();
Decrement D2(100);
D2.print();
--D2;
D2.print();
return 0;
}
C++ code for Unary Operator Overloading (++)
This is the solution that increases an integer value by 1 by overloaded operator (T.U. 2073)
#include <iostream>
using namespace std;
class Increment
{
int a;
public:
Increment()
{
a=0;
}
Increment(int x)
{
a=x;
}
operator++(int)
{
a++;
}
operator++()
{
++a;
}
void print();
};
void Increment::print()
{
cout << endl << "a=" << a << endl ;
}
int main()
{
Increment I1(4);
I1.print();
I1++;
I1.print();
Increment I2(100);
I2.print();
++I2;
I2.print();
return 0;
}
C++ code to Convert Centigrade to Fahrenheit
This is the solution to convert Centigrade into Fahrenheit (T.U. 2066)
#include <iostream>
using namespace std;
class Temperature
{
float centigrade, fahrenheit ;
public:
Temperature()
{
centigrade=0.0;
}
void GetTemp();
void Conversion();
void print();
};
void Temperature::GetTemp()
{
cout << endl << "Enter temperature in centigrade:" ;
cin >> centigrade;
}
void Temperature::Conversion()
{
fahrenheit = ((9*centigrade)/5) + 32 ;
}
void Temperature::print()
{
cout << endl << "Temperature in Centigrade=" << centigrade <<
endl << "Temperature in Fahrenheit=" << fahrenheit << endl ;
}
int main()
{
Temperature t1;
t1.GetTemp();
t1.Conversion();
t1.print();
return 0;
}
C++ code for Word Count of entered Line of Text
This is the solution for counting the number of Words in given String in C++ (T.U. 2066)
#include <iostream>
#include<string.h>
using namespace std;
class WordCount
{
string s;
int counts;
public:
WordCount()
{
counts=1;
}
void GetString();
void ReadCount();
void print();
};
void WordCount::GetString()
{
cout << endl << "Type your text:" ;
getline(cin,s);
}
void WordCount::ReadCount()
{
if(s[0]==' ')
{
counts=1;
}
for(int i=1; i<s.length(); i++)
{
if(s[i]==' ')
{
if(s[i+1]==' ')
{
counts = counts ;
}
else
{
counts++;
}
}
}
}
void WordCount::print()
{
cout << endl << "The string you entered is: " ;
cout << s ;
cout << endl << "Word Count=" << counts << endl ;
}
int main()
{
WordCount wc;
wc.GetString();
wc.ReadCount();
wc.print();
return 0;
}
C++ code for finding the Cube of a Number using Inline Function
This is the solution for finding Cube of a given number using Inline Function in C++ (T.U. 2066)
#include <iostream>
using namespace std;
class cube
{
int a, acube;
public:
void geta();
inline void cubea();
void show();
};
void cube::geta()
{
cout << endl << "Enter a number:" ;
cin >> a ;
}
void cube::cubea()
{
acube = a*a*a ;
}
void cube::show()
{
cout << endl << "Entered number(a)=" << a << endl << "Cube of a=" << acube << endl ;
}
int main()
{
cube c1;
c1.geta();
c1.cubea();
c1.show();
return 0;
}
C++ code to Convert Feet into Meter
This is the solution to convert Feet into Meter (T.U. 2067)
#include <iostream>
using namespace std;
class Feet2Meter
{
float feet, meter ;
public:
void GetFeet();
void Conversion();
void print();
};
void Feet2Meter::GetFeet()
{
cout << endl << "Enter distance in feet:" ;
cin >> feet ;
}
void Feet2Meter::Conversion()
{
meter = 0.3048*feet ;
}
void Feet2Meter::print()
{
cout << endl << "Distance in feet=" << feet << endl << "Distance in meter=" << meter ;
}
int main()
{
Feet2Meter f;
f.GetFeet();
f.Conversion();
f.print();
return 0;
}
C++ code to Convert Inch into Centimeter
This is the solution to convert Inch into Centimeter in C++ (T.U. 2068)
#include <iostream>
using namespace std;
class InchtoCentimeter
{
float Inch, Centimeter ;
public:
void GetInch();
void Conversion();
void print();
};
void InchtoCentimeter::GetInch()
{
cout << endl << "Enter inch:" ;
cin >> Inch ;
}
void InchtoCentimeter::Conversion()
{
Centimeter = 2.54*Inch ;
}
void InchtoCentimeter::print()
{
cout << endl << "Inch=" << Inch << endl << "Centimeter=" << Centimeter << endl ;
}
int main()
{
InchtoCentimeter d;
d.GetInch();
d.Conversion();
d.print();
return 0;
}
C++ code for finding the Square of a Number using Inline Function
This is the solution for finding Square of a number using Inline Function (T.U. 2067, 2068)
#include <iostream>
using namespace std;
class square
{
int a, asqr ;
public:
void geta();
inline void squarea();
void print();
};
void square::geta()
{
cout << endl << "Enter a:" ;
cin >> a ;
}
void square::squarea()
{
asqr = a*a ;
}
void square::print()
{
cout << endl << "Entered number(a)=" << a << endl << "Square of a=" << asqr << endl ;
}
int main()
{
square s1;
s1.geta();
s1.squarea();
s1.print();
return 0;
}
C++ code for subtracting Complex Numbers by Operator Overloading
This is the C++ code for subtracting two Complex Numbers by Overloading Binary(-) Operator (T.U. 2066)
#include <iostream>
using namespace std;
class Complex
{
float real, img ;
public:
Complex()
{
real=0;
img=0;
}
Complex(int x, int y)
{
real = x;
img = y;
}
Complex operator-(Complex);
void print();
};
void Complex::print()
{
cout << endl << real << "+i" << img << endl ;
}
Complex Complex::operator-(Complex c1)
{
Complex c;
c.real = real-c1.real ;
c.img = img-c1.img ;
return c;
}
int main()
{
Complex c1(4,6), c2(6,4);
Complex c3;
c3 = c1-c2 ;
cout << endl << "First complex number=" << endl ;
c1.print();
cout << endl << "Second complex number=" << endl ;
c2.print();
cout << endl << "Difference=" << endl ;
c3.print();
return 0;
}
C++ code for Unary Operator Overloading (--)
This is the solution that decreases an integer value by 1 by overloaded operator (T.U. 2071)
#include <iostream>
using namespace std;
class decrement
{
int a;
public:
decrement()
{
a=0;
}
decrement(int x)
{
a=x;
}
operator--(int)
{
a--;
}
operator--()
{
--a;
}
void print();
};
void decrement::print()
{
cout << endl << "a=" << a << endl ;
}
int main()
{
decrement d1(4);
d1.print();
d1--;
d1.print();
decrement d2(100);
d2.print();
--d2;
d2.print();
return 0;
}
Subscribe to:
Posts (Atom)