Professional Documents
Culture Documents
#include <iostream>
using namespace std;
class Engine {
public:
Engine(int nc) :cylinder(nc){}
void start() {
cout << getCylinder() << " cylinder engine started" ;
};
int getCylinder() {
return cylinder;
}
private:
int cylinder;
};
class Car : private Engine
{ // Car has-a Engine
public:
// Define the constructor. Name of the parameter should be 'nc'
Car(int nc):Engine(nc){}
void start() {
cout << "car with " << Engine::getCylinder() <<
" cylinder engine started" << endl;
#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
string Name;
double salary;
Employee(string fName, double sal) : Name(fName), salary(sal) {}
void show() {
cout << Name << " " << salary;
}
void addBonus(double bonus) {
salary += bonus;
}
};
class Manager :public Employee {
public:
Manager(string fName, double sal) : Employee(fName, sal) {}
};
class Clerk :public Employee {
public:
Clerk(string fName, double sal) : Employee(fName, sal) {}
};
void congratulate(Employee* emp) {
emp->addBonus(200);
emp->show();
cout << " ";
};
int main() {
Employee* emp;
int sal_m, sal_c;
cin >> sal_c >> sal_m;
Manager m1("Steve", sal_m);
Clerk c1("Kevin", sal_c);
// Call the proper function/s to congratulate the Manager and the Clerk
congratulate(&c1);
congratulate(&m1);
return 0;
}
#include <iostream>
using namespace std;
class Base {
public:
virtual void DoIt() = 0; // pure virtual
virtual ~Base() {};
};
Base* CreateRandom(int x) {
if ((x % 2) == 0)
return new Foo;
else
return new Bar;
}
int main() {
int lim = 0;
cin >> lim;
for (int n = 0; n < lim; ++n) {
#include <iostream>
#include <vector>
using namespace std;
class Test {
static int count;
int id;
public:
Test(int id) {
count++;
cout << count << ' ';
if (count == id)
throw id;
}
~Test() {}
};
int Test::count = 0;
int main() {
int n, m = 0;
cin >> n >> m;
vector<Test> testArray; // Using STL vector, declare testArray
try {
for (int i = 0; i < n; ++i) {
testArray.push_back(Test(m));
}
}
catch (int i)// Write the catch clause
{
cout << "Caught " << i << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main(){
int d;
int *p = (int *)operator new(sizeof(int)); // Declare variable 'p' and use operator new to
// allocate memory to it
cin >> d ;
*p = d ;
cout << ++*p + d++;
return 0;
}
#include<iostream>
using namespace std;
class MyClass {
public:
static int count;
MyClass(){ count++; }
~MyClass() {cout << --count << ","; } //Write the definition of the destructor
}; // End of Myclass
int MyClass::count = 1;
int main() {
cin >> MyClass::count;
MyClass *pt;
pt = new MyClass[2];
delete[] pt;
return 0;
}
include <iostream>
using namespace std;
class Area {
public:
int area;
int calc(int l, int b) { area = 4 *l*b; return area; }
};
class Perimeter {
public:
int peri;
int calc(int l, int b) {peri = 8 * (l + b); return peri; }
};
private:
int length, breadth;
int area() {
/* Calls area_calc() of class Area and returns it. */
return Area::calc(length, breadth);
}
int peri() {
/* Calls peri_calc() function of class Perimeter and returns it. */
return Perimeter::calc(length, breadth);
}
public:
Rectangle(int l, int b) : length(l), breadth(b) {}
int print() {
cout << area() << " " << peri();
}
};
int main() {
int l, b;
cin >> l >> b;
#include <iostream>
using namespace std;
class A {
int n;
protected:
A(int i) : n(i) { }
virtual void print() = 0;
virtual int get(){ return n; }
};
class B : private A {
public:
B(int i) : A(i) {}
int get() {
A::get(); // The get function body
}
};
class C : public B {
public:
C(int i) : B(i) {}
void print() {
cout << B::get() << endl; // display the result of the get function
}
};
int main(){
int n;
cin >> n;
C *p = new C(n);
p->print();
return 0;
}