Program1
#include
#include
using namespace std;
class p
{
public:
void print()
{
cout <<" Inside p::";
}
};
class q : public p
{
public:
void print()
{
cout <<" Inside q";
}
};
class r: public q
{
};
int main(void)
{
r r;
r.print();
return 0;
}
Output
Inside
Q
Program2
#include
#include
using namespace std;
class base
{
public:
base()
{
fun();
}
virtual void fun()
{
cout<<"\nBase Function";
}
};
class derived: public base
{
public:
derived(){}
virtual void fun()
{
cout<<"\nDerived Function";
}
};
int main()
{
base* pbase = new derived();
delete pbase;
return 0;
}
Output
Base
Function
Program3
#include
#include
using namespace std;
int main()
{
int sumodd = 0;
int sumeven = 0;
int upperbound;
int absdiff;
cout << "Enter the upperbound: ";
cin >> upperbound;
int number = 1;
while (number <= upperbound)
{
if (number % 2 == 0)
{
sumeven += number;
}
else
{
sumodd += number;
}
++number;
}
if (sumodd > sumeven)
{
absdiff = sumodd - sumeven;
}
else
{
absdiff = sumeven - sumodd;
}
cout << "The sum of odd numbers is " << sumodd << endl;
cout << "The sum of even numbers is " << sumeven << endl;
cout << "The absolute difference is " << absdiff << endl;
return 0;
}
Output
Enter the upperbound: 1000
The sum of odd numbers is 250000
The sum of even numbers is 250500
The absolute difference is 500
Program 4
#include
#include
using namespace std;
int main()
{
int mark = 70;
if (mark >= 50)
{
cout << "You Pass!" << endl;
}
else
{
cout << "You Fail!" << endl;
}
return 0;
}
Program 5
#include
#include
#include
using namespace std;
int main()
{
int sum = 0;
double average;
for (int number = 1; number <= 100; ++number)
{
sum += number;
}
average = sum / 100;
cout << fixed << setprecision(1);
cout << "Average is " << average << endl;
return 0;
}
No comments:
Post a Comment