KEMBAR78
CS304 OOP in C++, Syntax | PDF | Class (Computer Programming) | Programming Paradigms
0% found this document useful (0 votes)
8 views6 pages

CS304 OOP in C++, Syntax

MIDTERM

Uploaded by

l.nadeem00051
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)
8 views6 pages

CS304 OOP in C++, Syntax

MIDTERM

Uploaded by

l.nadeem00051
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/ 6

CS304 Object oriented Programming

1. Class Declaration

Definition: Blueprint for creating objects.

// Syntax
class ClassName {
// data members
// member functions
};

// Example
class Car {
public:
void drive() {
cout << "Driving..." << endl;
}
};

2. Object Creation

Definition: Creating an instance of a class.

// Syntax
ClassName objName;

// Example
Car myCar;
myCar.drive(); // Output: Driving...

3. Constructor

Definition: Special function called automatically when an object is created.

// Syntax
class ClassName {
public:
ClassName() { // Constructor
// code
}
};

// Example
class Car {
public:
Car() {
cout << "Car created\n";
}
};
Car c; // Output: Car created

4. Destructor

Definition: Special function called automatically when an object is destroyed.

// Syntax
~ClassName();

// Example
class Car {
public:
~Car() {
cout << "Car destroyed\n";
}
};

5. Access Specifiers

Definition: Control access level to class members.

// Syntax
class MyClass {
private:
int a; // accessible only inside class
protected:
int b; // accessible in derived class
public:
int c; // accessible everywhere
};

6. Encapsulation

Definition: Wrapping data and functions into a single unit (class).

// Example
class Bank {
private:
int balance;
public:
void setBalance(int b) { balance = b; }
int getBalance() { return balance; }
};

7. Inheritance
Definition: Acquiring properties of one class into another.

// Syntax
class Derived : access Base {
// body
};

// Example
class Animal {
public:
void speak() { cout << "Animal speaks\n"; }
};

class Dog : public Animal {


};
Dog d;
d.speak(); // Output: Animal speaks

8. Multiple Inheritance

Definition: A class inherits from more than one base class.

// Syntax
class Derived : public Base1, public Base2 {
};

// Example
class A {
public:
void showA() { cout << "A\n"; }
};
class B {
public:
void showB() { cout << "B\n"; }
};
class C : public A, public B {
};
C obj;
obj.showA(); // A
obj.showB(); // B

9. Function Overloading

Definition: Same function name with different parameters.

// Example
class Print {
public:
void show(int a) { cout << "Int: " << a << endl; }
void show(double b) { cout << "Double: " << b << endl; }
};
Print p;
p.show(5); // Int: 5
p.show(5.5); // Double: 5.5

10. Function Overriding

Definition: Redefining base class function in derived class.

// Example
class Base {
public:
void show() { cout << "Base\n"; }
};
class Derived : public Base {
public:
void show() { cout << "Derived\n"; }
};
Derived d;
d.show(); // Output: Derived

11. Polymorphism

Definition: Ability to take many forms (Compile-time and Runtime).

// Compile-time: function overloading


// Runtime: function overriding with pointers

class Animal {
public:
virtual void speak() { cout << "Animal\n"; }
};
class Dog : public Animal {
public:
void speak() override { cout << "Dog\n"; }
};

Animal* a = new Dog();


a->speak(); // Output: Dog (runtime polymorphism)

12. Virtual Functions

Definition: Functions meant to be overridden in derived classes.

// Example
class Base {
public:
virtual void display() { cout << "Base\n"; }
};
class Derived : public Base {
public:
void display() override { cout << "Derived\n"; }
};
Base* b = new Derived();
b->display(); // Output: Derived

13. Abstract Class

Definition: Class with at least one pure virtual function.

// Syntax
class Abstract {
public:
virtual void show() = 0; // pure virtual function
};

// Example
class Derived : public Abstract {
public:
void show() { cout << "Implementation\n"; }
};

14. This Pointer

Definition: Pointer to the current object.

// Example
class MyClass {
private:
int x;
public:
void setX(int x) {
this->x = x;
}
};

15. Friend Function

Definition: Non-member function with access to private members.

// Example
class Box {
private:
int width;
public:
Box() { width = 10; }
friend void printWidth(Box b);
};
void printWidth(Box b) {
cout << "Width is: " << b.width << endl;
}

16. Static Members

Definition: Shared among all objects of a class.

// Example
class Demo {
public:
static int count;
Demo() { count++; }
};

int Demo::count = 0;

17. Operator Overloading

Definition: Giving special meaning to an operator.

// Example
class Complex {
public:
int real, imag;
Complex(int r, int i) : real(r), imag(i) {}
Complex operator + (const Complex& obj) {
return Complex(real + obj.real, imag + obj.imag);
}
};

18. Namespace

Definition: Used to avoid naming conflicts.

// Example
namespace first {
void show() { cout << "First\n"; }
}
namespace second {
void show() { cout << "Second\n"; }
}
first::show(); // First

You might also like