Object Oriented Programming Unit-1b Basic Theory

  • In Object-oriented programming (OOP), classes and objects have attributes. Attributes are data stored inside a class or instance and represent the state or quality of the class or instance. One can think of attributes as noun or adjective, while methods are the verb of the class.
  • Attributes - things that the object stores data in, generally variables.
  • Methods - Functions and Procedures attached to an Object and allowing the object to perform actions.

A class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside the curly brackets and terminated by a semicolon at the end.

  • When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, objects are required to create.
  • Syntax:
    • ClassName ObjectName;

  • The data members and member functions of class can be accessed using the dot (‘.’) operator with the object.
  • For example,
    • if the name of object is obj and to access the member function with the name printName()
    • then obj.printName() is required to write.

  • The public data members are also accessed in the same way given however the private data members are not allowed to be accessed directly by the object.
  • Accessing a data member depends solely on the access control of that data member.
  • This access control is given by Access modifiers in C++.

  • There are three access modifiers :
    1. public
    2. private
    3. protected

  • #include<iostream.h>
  • #include<conio.h>
  • int main ()
  • {
    1. clrscr();
    2. int a;
    3. cin>>a;
    4. a = a+1;
    5. a = a+1;
    6. a = a+1;
    7. cout<<a;
    8. getch();
    9. return 0;
  • }

  • #include<iostream>
  • using namespace std;
  • int main ()
  • {
    1. int a;
    2. cin>>a;
    3. a = a+1;
    4. cout<<a;
    5. return 0;
  • }

  • #include <iostream>
  • using namespace std;
  • int main()
  • {
    1. int x = 5;
    2. int y = 6;
    3. int sum = x + y;
    4. cout << sum;
    5. return 0;
    6. }

  • #include <iostream>
  • using namespace std;
  • int main()
  • {
    1. float x , y ,Sum, Avg;
    2. cin>>x>>y;
    3. Sum = X + y;
    4. Avg = Sum / 2;
    5. cout << "Sum ="<< Sum<<endl;
    6. cout <<"Avg ="<< Avg << endl;
    7. return 0;
  • }

  • The cascading of the input and output operators refers to the consecutive occurrence of input or output operators in a single statement.
  • For Example:
  • cin>>a>>b;
  • cout<<"The value of b is : "<<b;
  • cout << "The value of a is "<< a;

  • Using the using keyword, it means read things by default.
  • Say: If there is an object name that doesn't exist in current namespace, then check if there exists a namespace std in which it does exist, and use that object.
  • Thus, it doesn't really add a function, it is the include that "loads" cout, cin, endl and all the like.

The data type specifies the size and type of information the variable will store:

Write a program to make a general machine (i.e. class) and display a message WELCOME through its Mobile Device (i.e. Object).

  • #include<iostream.h>
  • //Create Class
  • class GenMachine
  • {
    • public:
    • void Display()
    • { cout<<”WELCOME”; }
  • };
  • void main()
  • {
    • //Create Object
    • class Machine Mobile;
    • Mobile.Display();
  • }

Write a program to create a Number class contain two integer number A and B and using Natural Number object read its value, do its sum and display its value and their sum.

  • #include <iostream.h>
  • //Create Class class Number
  • {
  • private: int A,B,Sum;
  • public:
  • void ReadData()
  • {
  • cout<<"Enter A and B";
  • cin>> A>>B;
  • }
  • void SumData()
  • {
  • Sum = A+B;
  • }
  • void DisplayData()
  • {
  • cout <<"A=" << A << endl << "B=" << B << endl << "Sum=" << Sum;
  • }
  • };
  • void main()
  • {
  • //Create Object
  • class Number NaturalNo;
  • NaturalNo.ReadData();
  • NaturalNo.DisplayData();
  • NaturalNo.DisplayData();
  • }

C++ is object oriented programming language. It provides a lot of features that are given below. Simple
  1. Machine Independent or Portable
  2. Mid-level programming language
  3. Structured programming language
  4. Rich Library
  5. Memory Management
  6. Fast Speed
  7. Pointers
  8. Recursion
  9. Extensible
  10. Object Oriented
  11. Compiler based

  • C++ is a simple language in the sense.
  • C++ provides structured approach (to break the problem into parts).
  • C++ provides rich set of library functions.
  • C++ provides data types.

Unlike assembly language, C programs can be executed in many machines with little bit or no change. But it is not platform-independent.

  • C++ is also used to do low level programming.
  • C++ is used to develop system applications such as kernel, driver etc.
  • C++ also supports the feature of high level language.
  • That is why C++ is known as mid-level language.

C++ is a structured programming language in the sense that we can break the program into parts using functions. So, it is easy to understand and modify.

C++ provides a lot of inbuilt functions that makes the development fast.

It supports the feature of dynamic memory allocation. In C++ language, we can free the allocated memory at any time by calling the free() function.

It supports the feature of dynamic memory allocation. In C++ language, we can free the allocated memory at any time by calling the free() function.

The compilation and execution time of C++ language is fast.

C++ provides the feature of pointers. We can directly interact with the memory by using the pointers. We can use pointers for memory, structures, functions, array etc.

In C++, we can call the function within the function. It provides code reusability for every function.

C++ language is extensible because it can easily adopt new features.

  • In C, a variable must be initialized using a constant expression, and the C complier would fix the initialization code at the time of compliation.
  • C++, however, permits initialization of the variables at run time. This is referred to as dynamic iniutalization.
  • In C++, a variable can be initializesd at run time using expressions at the place of declaration.
  • For Example:
    • int a;
    • cin>>a;
    • int b = a + 10;
    • cout <<a << " " <<b;
    • int R = 5;
    • float area = 3.14 * R * R;

C++ introduces a new kind of variables know as the reference variable. A reference variable provides an alias (alternative name) for a previously defined variable. For Example:
  • float total = 100; float &Sum = total;
  • cout<<"total="<< total << "Sum="<< Sum << endl;
  • total = total + 100;
  • cout<<"total=" << total <<"Sum=" << Sum<< endl;
  • Sum = Sum + 100;
  • cout<<"total="<< total<<" Sum="<<"Sum<<"endl;

  • A public member function can also be defined outside of the class with a special type of operator known as Scope Resolution Operator (SRO); SRO represents by :: (double colon)
  • Let’s consider the syntax
  • return_type class_name::function_name(parameters)
  • {
  • function_body;
  • }

  • #include<iostream.h>
  • //Create Class class Machine
  • {
    1. public: void Display();
  • };
  • void Machine::Display()
  • {
    1. cout<<"WELCOME";
  • }
  • void main()
  • {
    1. //Create Object
    2. class Machine Computer, Mobile;
    3. Computer.Display();
    4. Mobile.Display();
  • }

  1. #include <iostream>
  2. using namespace std;
  3. class Demo
  4. {
    • private:
      • int a, b;
    • public:
      • void setOneNo(int x)
        • { a = x; }
      • void dispOneNo()
        • { cout<<"a = "<< a << endl; }
      • void setTwoNo(int x, int y)
        • { a = x; b = y; }
      • void dispTwoNo()
        • { cout<<"a = "<< a << endl; cout<<"b = "<< b << endl;}
  5. };
  6. int main()
  7. {
    • //object declarations Demo d1;
    • Demo d2;
    • //assigning values to the data member of objects d1.setOneNo(10);
    • d2.setTwoNo(20,30);
    • //printing the values d1.dispOneNo(); d2.dispTwoNo();
    • return 0;
  8. }

  1. #include
  2. using namespace std;
  3. class Addition
  4. {
    • private:
      • int a, b;
    • public:
      • void readData(int x, int y)
      • {
        1. a = x;
        2. b = y;
      • }
      • void writeData()
      • {
        1. cout<<"a = "<< a << endl;
        2. cout<<"b = "<< b << endl;
      • }
      • int AddData()
      • {
        1. return a+b;
      • }
  5. };
  6. int main()
  7. {
    1. //object declarations
    2. Addition Numbers;
    3. //assigning values to the data member of objects
    4. Numbers.readData(10,20);
    5. Numbers.AddData();
    6. cout<< Numbers.AddData();
    7. return 0;
  8. }

  1. #include<iostream>
  2. using namespace std;
  3. class Factorial
  4. {
  5. private:
    • int i, F ;
  6. public:
    • int Fact(int N)
    • {
    • F = 1;
    • for(i=N; i>1 ; i--) F = F * i;
    • return F;
    • }
  7. };

  8. int main()
  9. {
  10. //object declarations
  11. Factorial Numbers;
  12. cout<< Numbers.Fact(5);
  13. return 0;
  14. }