1 / 12

An overview of C++

An overview of C++. C++ is an enhanced version of the C language. It includes everything that is part of C and adds support for object-oriented programming(OOP for short) Evolution of programming approach: Toggling switches on the front panel of the computer (smallest programs)

nora
Download Presentation

An overview of C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. An overview of C++ • C++ is an enhanced version of the C language. It includes everything that is part of C and adds support for object-oriented programming(OOP for short) • Evolution of programming approach: • Toggling switches on the front panel of the computer (smallest programs) • Assembly language (longer programs) • High-level language, i.e. FOTRAN (several thousand lines long programs) • Structured programming language, i.e. Pascal, C (50,000 lines programs) • Object-Oriented Programming

  2. OOP • Allows more complex and larger size programs to be written • It decomposes a problem into subgroups. Each subgroup becomes a self-contained object that contains its own instructions and data that relate to the object • All OOP languages share three common defining traits: • Encapsulation • Polymorphism • Inheritance

  3. Encapsulation • A mechanism that binds together code and the data it manipulates, and keeps the “black box” safe from outside inference and misuse • This “black box” is actually called object • Within an object, code, data, or both may be private to that object or public • Private code or data is known to and accessible only by another part of the object • Public code or data is also known to and accessible by other part of the program • An object is a variable of a user-defined type

  4. Polymorphism • Allows one name to be used for two or more related but technically different purposes. • In OOP, it allows one name to be used to specify a general class of actions (but the specific action is determined by type of data) • For Example, a common name abs() is used for abs(), fabs() and labs(). It is called function overloading • The main idea is “one interface, multiple methods” • Arithmetic operator + can be used for integers, float, characters as well as strings and other user defined data

  5. Inheritance • A process by which one object can acquire the properties of another • Allows an object to support hierarchical classification • Example: house->building->structure->man-made • Child class inherits all those qualities associated with the parent and adds to them its own defining characteristics

  6. Classes: A First Look • Class is the mechanism that is used to create objects • Syntax of class declaration: class class-name{ private functions and variables of the class public: public function and variables of the class } object-list;//object list is optional

  7. Classes: A First Look(Contd) • While the class-name is also technically optional, from a practical point of view, it is virtually always needed. Because it becomes a new type name that is used to declare objects of the class • Functions and variables declared inside a class declaration are said to be members of that class • By default, all functions and variables declared inside a class are private that class • To declare public members, public keyword is used

  8. Classes: A First Look (Contd) • Ex: class myclass{ int a; public: void set_a(int num); int get_a(); }; • Since a is private, it is not accessible by any code outside myclass. But only set_a() and get_a() member functions can access a • As set_a() and get_a() are declared public, they can be access from any part of the program

  9. Classes: A First Look (Contd) • Here functions get_a() and set_a() are declared, but not defined. Here is the definition: void myclass::set_a(int num) { a=num; } int myclass::get_a() { return a; } • :: is called the scope resolution operator

  10. Classes: A First Look (Contd) • Declaration of myclass did not define any object of type myclass—it only defines the types of object that will be created when one is actually declared. Objects are declared in the following way: myclass ob1, ob2; • Within the program, objects public members can be accessed in the following way: ob1.set_a(10); ob2.set_a(99);

  11. Classes: A First Look (Contd) • To display a’s value for each object: cout<<ob1.get_a(); cout<<ob2.get_a(); • What happens if we write following statement? ob1.a=10; ob2.a=99; a compiler-time error will result • It is only possible if a is declared public member variable

  12. Example • Implementation of stack by class • Private member variables: stck[SIZE] and tos • Public members functions: init(), push(char c) and pop() • Initialization can be done by constructor function (having the same name of the class)

More Related