Looking for a Tutor Near You?

Post Learning Requirement »
x
x

Direction

x

Ask a Question

x

Hire a Tutor

Object Oriented Programming

Loading...

Published in: C / C++
1,661 Views

It will help students to understand the oop concepts of c++. Really beneficial for all engineering and 11/12th students.

Prerna S / Sharjah

4 years of teaching experience

Qualification: B.E in computer Science

Teaches: GRE, SAT, ACT, Maths, MS Office, Web Development, HTML, Java, English, Hindi, Science, Chemistry, Computer Science, Mathematics, English Language

Contact this Tutor
  1. Object Oriented Programming
  2. What is an object ? object in C++ as, "a piece of memory that can be used to store values". An object with a name is called a variable. Take a look around you -- everywhere you look are objects: books and buildings and food and even you. Objects have two major components to them: 1) A list of relevant properties (e.g. weight, color, size, solidity, shape, etc...), and 2) Some number of behaviors that they can exhibit (e.g. being opened, making something else hot, etc...). These properties and behaviors are inseparable.
  3. ' Object-oriented programming (OOP) provides us with the ability to create objects that tie together both properties and behaviors into a self-contained, reusable package.
  4. What are classes? ' classes are very much like data-only structs, except that classes provide much more power and flexibility. class DateClass public: int m _ year; int m month; int m_day;
  5. instansiation ' Just like with a struct, to use a class, a variable of that class type must be declared: DateClass today { 2020, 10, 14 }; // declare a variable of class DateClass
  6. Member functions classes can also contain functions! Functions defined inside of a class are called member functions (or sometimes methods). Member functions can be defined inside or outside of the class definition. class DateClass public: int m _ year; int m month; int m _ day; void print() // defines a member function named print() std::cout
  7. int main() DateClass today { 2020, 10, 14 }; today.m day = 16; // use member selection operator to select a me mber variabe of the class today.print(); // use member selection operator to call a member fun ction of the class return O; Rule: Name your classes starting with a capita/ letter.
  8. Practise: ' Define a class named Employee ,with three properties name ,id and wage and a function called print ().declare 2 emplyees with name riddhi and samriddhi and print their values . ' Hint:use string for the employee name eg :string name;
  9. Public vs private access specifiers ' Public members are members of a struct or class that can be accessed from outside of the struct or class ' Private members are members of a class that can only be accessed by other members of the class
  10. class DateCIass // members are private by default int m _ month; // private by default, can only be accessed by other members int m _ day; // private by default, can only be accessed by other members int m _ year; // private by default, can only be accessed by other members int main() DateCIass date; date.m_month = 10; // error date.m_day = 14; // error date.m_year = 2020; // error return O;
  11. ' Although class members are private by default, we can make them public by using the public keyword: C++ provides 3 different access specifier keywords: public, private, and protected. Public and private are used to make the members that follow them public members or private members respectively ' An access specifier determines who has access to the members that follow the specifier.
  12. Why make member variables private? ' In modern life, we have access to many electronic devices. Your TV has a remote control that you can use to turn the TV on/off. You drive a car to work. You take a picture on your digital camera. All three of these things use a common pattern: They provide a simple interface for you to use (a button, a steering wheel, etc...) to perform an action. However, how these devices actually operate is hidden away from you. When you press the button on your remote control, you don't need to know what it's doing to communicate with your TV. When you press the gas pedal on your car, you don't need to know how the combustion engine makes the wheels turn. When you take a picture, you don't need to know how the sensors gather light into a pixellated image. This separation of interface and implementation is extremely useful because it allows us to use objects without understanding how they work. This vastly reduces the complexity of using these objects, and increases the number of objects we're capable of interacting with.
  13. Encapsulation ' In object-oriented programming, Encapsulation (also called information hiding) is the process of keeping the details about how an object is implemented hidden away from users of the object.
  14. constructor ' A constructor is a special kind of class member function that is automatically called when an object of that class is instantiated. Constructors are typically used to initialize member variables of the class to appropriate default or user-provided values, or to do any setup steps necessary for the class to be used (e.g. open a file or database). constructors have specific rules for how they must be named: ' Constructors must have the same name as the class (with the same capitalization) ' Constructors have no return type (not even void)
  15. Default constructors ' A constructor that takes no parameters (or has parameters that all have default values) is called a default constructor. The default constructor is called if no user-provided initialization values are provided.
  16. class Fraction private: int numerator; int denominator; public: Fraction() // default constructor numerator = O; denominator = 1; int getNumerator() { return numerator; } int getDenominator() { return denominator; } double getValue() { return static_cast(numerator) / denominator; }
  17. int main() Fraction frac; // Since no arguments, calls Fraction() default construct or std::cout
  18. implicit constructor ' If your class has no other constructors, C++ will automatically generate a public default constructor for you. This is sometimes called an implicit constructor (or implicitly generated constructor). ' If your class has any other constructors, the implicitly generated constructor will not be provided.
  19. practise ' 1) Write a class named Ball. Ball should have two private member variables with default values: m _ color ("black") and m _ radius (10.0). Ball should provide constructors to set only m _ color, set only m radius, set both, or set neither value. do not use default parameters for your constructors. Also write a function to print out the color and radius of the ball.
  20. delegating constructors ' To have one constructor call another, simply call the constructor in the member initializer list. This is one case where calling another constructor directly is acceptable.
  21. class Foo private: public: FOO() // code to do A Foo(int value): Foo() // use Foo() default constructor to do A // code to do B
  22. destructor ' A destructor is another special kind of class member function that is executed when an object of that class is destroyed. ' the class destructor is automatically called (if it exists) to do any necessary clean up before the object is removed from memory. ' Destructor naming ' Like constructors, destructors have specific naming rules: 1) The destructor must have the same name as the class, preceded by a tilde 2) The destructor can not take arguments. 3) The destructor has no return type.
  23. class IntArray private: int *m _ array; int m _ length; public: IntArray(int length) // constructor assert(length > 0); m _ array = new int[length]; m _ length = length; NIntArray() // destructor // Dynamically delete the array we allocated earlier delete[] m _ array ;
  24. Static variables A static duration variable (also called a "static variable") is one that retains its value even after the scope in which it has been created has been exited! Static duration variables are only created (and initialized) once, and then they are persisted throughout the life of the program.
  25. void incrementAndPrint() int value = 1; // automatic duration by default + -I-va e; std::cout
  26. void incrementAndPrint() static int s value = 1; // static duration via static keyword. This line is only executed onöe. ++s value; std::cout
  27. string int main() std::cout > age; std::cout
  28. Get line To read a full line of input into a string, you're better off using the std::getline() function instead. std::getline() takes two parameters: the first is std::cin, and the second is your string variable. int main() cout