












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













In Procedure Oriented Programming (POP), a program is divided into smaller parts called functions. In Object Oriented Programming (OOP), a program is divided into parts called objects.
In POP, importance is given to functions and the sequence of actions, not data. In OOP, objects can communicate with each other through member functions.
Adding new data and functions in POP is not easy. OOP provides an easier way to add new data and functions.
In POP, most functions use global data for sharing, which can be accessed freely. In OOP, data cannot move easily from function to function and can be kept public or private to control access.
Examples of OOP languages include C++, JAVA, VB.NET, and C#.NET. C++ was initially called 'C with Classes'.
Object Oriented Programming is a programming paradigm where applications are designed and developed based on objects. Objects are instances (variables) of a class.
An object is a collection of entities containing data and code to manipulate that data. Objects can interact without knowing the details of each other's data or code.
A class is a collection of objects of a similar type. It is a collection of data members and member functions. Once a class is defined, any number of objects belonging to that class can be created. For example, grapes, bananas, and oranges are members of the class fruit.
For example: Fruit orange; creates an object named orange that belongs to the class fruit. Classes are user-defined data types.
Combining data and functions into a single unit called a class is known as Encapsulation. A class contains both data and functions.
Hiding the complexity of a program and representing only essential features is called Abstraction.
Dynamic Binding refers to linking a function call with its definition at runtime.
Message Passing is the process by which one object interacts with another.
Inheritance is the process by which an object of one class acquires the properties or features of objects of another class. For example, a robin is part of the class flying bird, which is part of the class bird.
Polymorphism means that the behavior depends on the types of data used in the operation. Examples include Operator Overloading and Function Overloading.
Through inheritance, redundant code can be eliminated, and the use of existing classes can be extended, which is not possible in a procedure- oriented approach.
Data hiding helps programmers build secure programs that cannot be invaded by code in other parts of the program.
Multiple instances of an object can co-exist without interference.
It is possible to map objects in the problem domain to those in the program.
It is easy to partition work in a project based on objects.
Message passing techniques for communication between objects make interface descriptions with external systems simpler.
#include #include void main() { cout << "Hello this is C++"; getch(); }
Header files are included at the beginning of a C++ program, similar to C. cin and cout are similar to scanf and printf, but format specifiers are not needed.
If bytes flow from main memory to a device (printer, display screen, etc.), it is an output operation. If bytes flow from a device to main memory, it is an input operation.
One class can inherit from another in this section. To use the data and access functions defined in the class, objects need to be created. The syntax is ClassName ObjectName;.
Accessing Data Members and Member Functions
Data members and member functions of a class can be accessed using the dot (.) operator with the object. For example, if the object's name is obj, public data members can be accessed using the direct member access operator (.).
C++ Program Using Class
Class names must start with a capital letter and contain data variables and member functions.
class Abc { int i; //data variable void display() //Member Function { cout << "Inside Member Function"; } }; // Class ends here int main() { Abc obj; // Creating Abc class's object obj.display(); // Calling member function using class object }
There are two ways to define member functions: inside the class definition and outside the class definition.
Inside the Class Definition
Functions defined inside the class are treated as inline functions automatically if the function definition doesn't contain looping statements or complex multiple-line operations.
Outside the Class Definition
Functions should be declared inside the class to bind it to the class and indicate it as its member.
#include using namespace std; class car { private:
int car_number; char car_model[10]; public: void getdata() { cout << "Enter car number: "; cin >> car_number; cout << "\n Enter car model: "; cin >> car_model; } void showdata() { cout << "Car number is " << car_number; cout << "\n Car model is " << car_model; } }; // main function starts int main() { car c1; c1.getdata(); c1.showdata(); return 0; }
Output:
Enter car number : 9999 Enter car model : Sedan Car number is 9999 Car model is Sedan
Syntax for declaring a function outside of a class:
class class_name { ........ ........ public: return_type function_name (args); //function declaration }; //function Member Definition Section
This section is optional. It is used only when you want to define member functions outside the class. It contains the definition of the member functions that are declared inside the class.
void Demo:: input () { cout << "Enter Value of A:"; cin >> a; cout << "Enter Value of B:"; cin >> b;
void has three uses: to specify a return type, to indicate an empty argument list, and to declare generic pointers.
enum boolean { false, true }; // inside function enum boolean check;
#include enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday void main() { week today; today = Wednesday; cout << "Day " << today+1; getch(); }
Output: Day 4
#include using namespace std; enum seasons { spring = 34, summer = 4, autumn = 9, winter = 32}; int main() { seasons s; s = summer; cout << "Summer = " << s << endl; return 0; }
Output: Summer = 4
Arrays
In C++, the array dimension of a string should include the null character.
Pointers
A pointer stores the memory address of other variables, functions, or even other pointers. The syntax is datatype * ptr_name;.
int var = 10; int * ptr; ptr = &var;
Example: var =10, &var = address of var, ptr = address of var, *ptr = 10
Reference Variables
Reference variables provide different names to the same variable (ALIAS - one location, two names).
Data-type & reference-name = variable-name
Int & num = sum ;
int & means reference to a float variable. It must always be initialized when it is declared. The main application is passing arguments to functions.
#include int sum(int a, int b=10, int c=20); void main(){ /* In this case a value is passed as
Access specifiers (visibility modifiers) provide data hiding and security.
Public variables are visible to all classes. Private variables are visible only to the class to which they belong. Protected variables are visible only to the class to which they belong and any subclasses.
If you want other objects to access internal data but wish to control it, you would make it either private or protected and provide functions that can manipulate the data in a controlled way.
Encapsulation is the wrapping up of data and functions into a single unit (called a class). Other objects adhere to these interfaces to use the object without being concerned with how the object accomplishes it. The object interface consists of public methods and instantiated data.
#include #include class Square { private: int Num; public: void Get() { cout<<"Enter Number:"; cin>>Num; } void Display() { cout<<"Square Is:"<< Num * Num; } };
return 0; }
Abstraction Using Classes
Abstraction can be achieved using classes. A class, such as 'Sum', can contain private members (e.g., x, y, and z) that are only accessible by the member functions of that class.
#include class Sum { private: int x, y, z; // private variables public: void add() { cout<<'Enter two numbers: '; cin>>x>>y; z= x+y; cout<<'Sum of two number is: '<<z<<endl; } }; int main() { Sum sm; sm.add(); return 0; }
A constructor is a special member function of a class that is automatically called when an object of that class is created.
Example:
class integer { int m,n; public: integer()//declaring the constructor { m=25;n=96; cout<<m<<endl< <n; } }; Main() { Clrscr(); Integer i1;
Getch(); }
Constructors should be declared in the public section of the class.
Example:
#include #include class para_cons { int m,n; public: para_cons(int,int); //constructor declared void display() { cout<<'m ='<< m << '\n'; cout<<'n ='<< n << '\n'; } }; para_cons::para_cons(int x, int y) //constructor defined { m=x; n=y; } main() { clrscr(); para_cons obj1(10,20); //constructor called implicitly para_cons obj2=para_cons(100,200); //constructor called explicitly cout << '\n OBJECT 1' << '\n'; obj1.display(); cout << '\n OBJECT 2' << '\n'; obj2.display(); getch(); }
A constructor can have default arguments.
Example:
class complex { float real,imag; public: complex(float r,float i=0)//constructor declared { real=r; imag=i; } void display()
public: // User-Defined Constructor Test() { // Number of times constructor is called cCount++; cout << 'No. of Object created: ' << cCount << endl; } // User-Defined Destructor ~Test() { dCount++; cout << 'No. of Object destroyed: ' << dCount << endl; // Number of times destructor is called } }; // driver code int main() { Test t, t1, t2, t3; return 0; }
Output:
No. of Object created: 1 No. of Object created: 2 No. of Object created: 3 No. of Object created: 4 No. of Object destroyed: 1 No. of Object destroyed: 2 No. of Object destroyed: 3 No. of Object destroyed: 4
The destructor function is automatically invoked when the objects are destroyed. Its name is the same as the class name, preceded by a tilde (~). delete is typically used in the destructor to free memory from objects, especially when new is used in the constructor to allocate memory.
Inheritance is the mechanism of deriving a new class from an existing one. The existing class is the base class, and the new class is the derived class or subclass. Inheritance allows reusing and extending existing classes without modifying them, creating hierarchical relationships.
Example:
#include class Value { protected: int val; public: void set_values (int a) { val=a; } }; class Cube: public Value { public: int cube() { return (valvalval); } }; int main () { Cube cub; cub.set_values (5); cout << 'The Cube of 5 is::' << cub.cube() << endl; return 0; }
Result:
The Cube of 5 is:: 125
The Value class contains a protected member val. Protected members can be accessed in derived classes.
Multiple inheritance involves deriving a class from more than one base class. This allows the derived class to inherit members from multiple parent classes.
Syntax:
Class C: Public A, Public B { Body of D }
Example:
class student { protected: int rno,m1,m2; public: void get()
class M { public: void display() { cout<<'vimal \n'; } }; class N { public: void display() { cout<<'Vaiwala\n'; } } class P:public M,public N { public: void display(void) { M::display(); N::display(); } }; int main() { clrscr(); P p; p.display(); getch(); }
Output:
Vimal Vaiwala
Hierarchical inheritance is where multiple subclasses inherit from one base class. It involves deriving two or more classes from a single base class.
Syntax:
class A { }; class B : visibility_label A { }; class C : visibility_label A
Example:
class A { public: int val; void getvalue(int j) { val=j; } }; class B : public A { public: void square() { cout<<endl<<valval; } }; class C : public A { public: void cube() { cout<<endl<<valval*val; } void main() { int i; clrscr(); B b; C c; cin>>i; b.getvalue(i); b.square(); c.getvalue(i); c.cube(); getch(); }
Output:
2 4 8
Multilevel inheritance is where a derived class is derived from another derived class.
class stu { protected: int rno; public: { rno=a; void get_no(int a); } void put_no(void) { out<<'Roll no'<<rno<<'\n'; } }; class test:public stu { public: protected: float part1,part2; void get_mark(float x,float y) { part1=x; part2=y; } void put_marks() { cout<<'Marks obtained:'<<'part1='<<part1<<'\n'<<'part2='<<part2<<'\n'; } }; class sports { public:
protected: float score; void getscore(float s) { score=s; } void putscore(void) { cout<<'sports:'<<score<<'\n'; } }; class result: public test, public sports { public: float total; void display(void); void result::display(void) { total=part1+part2+score;
put_no(); put_marks(); putscore(); cout<<'Total Score='<<total<<'\n'; } int main() { clrscr(); result stu; stu.get_no(123); stu.get_mark(27.5,33.0); stu.getscore(6.0); stu.display(); return 0; }
OUTPUT:
Roll no 123 Marks obtained : part1=27.5 Part2= Sports= Total score = 66.