You are on page 1of 6

#include<iostream>

using namespace std;


class MyClass { int x_;
public:
MyClass(int i) : x_(i) {}
friend void display(MyClass a); // Declare the display function.
}; // End of class

void display(MyClass a) { // Fill the parameters


cout << " " << a.x_;
}
int main(){
int x;
cin >> x;
MyClass obj(x);
display(obj);
return 0;
}

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

Engine::start(); // Call the start function of Engine


}
};
int main()
{
int cylin;
cin >> cylin;
Car c(cylin);
c.start();
return 0;
}

#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() {};
};

class Foo : public Base {


public:
virtual void DoIt() { cout << "12,"; };
void FooIt() { cout << "13,"; }
};

class Bar : public Base {


public:
virtual void DoIt() { cout << "14,"; }
void BarIt() { cout << "15:"; }
};

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

Base* base = CreateRandom(n);


base->DoIt();
Bar* bar = dynamic_cast<Bar*>(base);
Foo* foo = dynamic_cast<Foo*>(base);
if (bar)
bar->BarIt();
if (foo)
foo->FooIt();
}
return 0;
}

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

operator delete(p); // Release the memory allocated above

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

/* Rectangle class is derived from classes Area and Perimeter. */


class Rectangle: public Area, public Perimeter { // Inherit the required base classes

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;

Rectangle r(l, b);


r.print();
return 0;
}

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

You might also like