Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Chapter - C++ Classes, PPT, C++ Programming, Semester, Engineering

Chapter - C++ Classes, PPT, C++ Programming, Semester, Engineering - Computer Science Engineering (CSE) PDF Download

From Procedural to Object-oriented Programming

•Procedural programming

       –Functions have been the main focus so far

            •Function parameters and return values

            •Exceptions and how the call stack behaves

        –We have viewed data and functions as being separate abstractions, for the most                  part

•Object-oriented (a.k.a. OO) programming

       –Allows us to package data and functions together

              •Makes data more interesting (adds behavior)

              •Makes functions more focused (restricts data scope)

       –This module will start to look at OO programing

               •Classes/structs, member operators/functions/variables

               •We’ll cover inheritance, polymorphism, substitution later

 

 

 

 

C++ Classes Concept------------------------------Next Slide----------------------------

 

 

 

 

 From C++ Functions to C++ Structs/Classes

•C++ functions encapsulate behavior

      –Data used/modified by a function must be passed in via parameters

      –Data produced by a function must be passed out via return type

 

•Classes (and structs) encapsulate related data and behavior

      –Member variables maintain each object’s state

      –Member functions (methods) and operators have direct access to member variables            of the object on which they are called

      –Class members are private by default, struct members public by default

 

•When to use a struct

      –Use a struct for things that are mostly about the data

      –Add constructors and operators to work with STL containers/algorithms

 

•When to use a class

       –Use a class for things where the behavior is the most important part

       –Prefer classes when dealing with encapsulation/polymorphism (later)

 

 

 

 

C++ Classes Concept------------------------------Next Slide----------------------------

 

 

 

Using Structs and Operators with STL Containers

#include <vector> // standard template library

#include <iostream>

using namespace std;

#include “point2d.h” // using user-defined code

 

// main function definition

int main (int, char *[]) {

  vector<Point2D> v; // must give a type here

  v.push_back(Point2D(2,3));

  v.push_back(Point2D(1,4));

 

  for (size_t s = 0; s < v.size(); ++s) {

    cout << v[s] << endl; // needs Point2D <<

  }

return 0;

}

 

 

 

C++ Classes Concept------------------------------Next Slide----------------------------

 

 

 

Using Structs and Operators with STL Algorithms

// same as before, and add algorithm library

#include <vector>

#include <algorithm>

using namespace std;

#include “point2d.h”

int main (int, char *[]) {

  vector<Point2D> v;

  v.push_back(Point2D(2,3));

  v.push_back(Point2D(1,4));

 

  // reorders the points in the vector

  // note that you don’t give a type here!

  sort (v.begin(), v.end()); // needs Point2D <

 

  return 0;

}

 

 

 

C++ Classes Concept------------------------------Next Slide----------------------------

 

 

 

Declaring and Defining C++ Structs/Classes

Chapter - C++ Classes, PPT, C++ Programming, Semester, Engineering - Computer Science Engineering (CSE)

 

 

 

 

C++ Classes Concept------------------------------Next Slide----------------------------

 

 

 

Structure of a Simple C++ Class Declaration

Chapter - C++ Classes, PPT, C++ Programming, Semester, Engineering - Computer Science Engineering (CSE)

 

 

 

C++ Classes Concept------------------------------Next Slide----------------------------

 

 

 

Constructors

Chapter - C++ Classes, PPT, C++ Programming, Semester, Engineering - Computer Science Engineering (CSE)

 

 

 

C++ Classes Concept------------------------------Next Slide----------------------------

 

 

 

 

A Bit More About Default Constructors

•Default constructor takes no arguments

      –Compiler synthesizes one if no constructors are provided

            •Does default construction of all class members (a.k.a member-wise)

      –If you write a default constructor

            •Can initialize default values via base/member list

            •Must do this for const and reference members

 

•Default construction of built-in types

       –Default construction does nothing (leaves uninitialized)

      –It’s an error (as of C++11) to read an uninitialized variable

 

 

 

C++ Classes Concept------------------------------Next Slide----------------------------

 

 

 

 

Access Control

•Declaring access control scopes within a class

    private: visible only within the class

   protected: also visible within derived classes (more later)

   public: visible everywhere

   –Access control in a class is private by default

       •but, it’s better style to label access control explicitly

•A struct is the same as a class, except

     –Access control for a struct is public by default

     –Usually used for things that are “mostly data”

            •E.g., if initialization and deep copy only, may suggest using a struct

    –Versus classes, which are expected to have both data and some form of non-trivial                 behavior

           •E.g., if reference counting, etc. probably want to use a class

 

 

 

C++ Classes Concept------------------------------Next Slide----------------------------

 

 

 

Issues with Encapsulation in C++

•Sometimes two classes are closely tied

       –For example, a container and its iterators

       –One needs direct access to the other’s internal details

       –But other classes shouldn’t have such direct access

       –Can put their declarations in the same header file

       –Can put their definitions in the same source file

•Poses interesting design forces

      –How should iterator access members of container?

      –Making container members public violates encapsulation

             •Any class, not just iterator could modify them

      –Make protected, derive iterator from container?

             •Could work: inheritance for implementation

             •But, may prove awkward if lots of classes/dependences are involved

      –Could have lots of fine-grain accessors and mutators

               •Functions to get and set value of each member variable

               •But may reduce encapsulation, clutter the interface for other classes

 

 

 

C++ Classes Concept------------------------------Next Slide----------------------------

 

 

 

 

Friend Declarations

•Offer a limited way to open up class encapsulation

•C++ allows a class to declare its “friends”

      –Give access to specific classes or functions

          •A “controlled violation of encapsulation”

      –Keyword friend is used in class declaration

•Properties of the friend relation in C++

     –Friendship gives complete access

           •Friend methods/functions behave like class members

           •public, protected, private scopes are all accessible by friends

     –Friendship is asymmetric and voluntary

          •A class gets to say what friends it has (giving permission to them)

          •But one cannot “force friendship” on a class from outside it

     –Friendship is not inherited

           •Specific friend relationships must be declared by each class

          •“Your parents’ friends are not necessarily your friends”

 

 

 

C++ Classes Concept------------------------------Next Slide----------------------------

 

 

 

Friends Example

Chapter - C++ Classes, PPT, C++ Programming, Semester, Engineering - Computer Science Engineering (CSE)

 

 

 

C++ Classes Concept------------------------------Next Slide-----------------------------

 

 

 

 

Operators let a Struct/Class Work with Entire STL

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

#include "point2d.h”

int main (int, char *[]) {

  vector<Point2D> v;

  v.push_back(Point2D(2,3)); v.push_back(Point2D(1,4));

  v.push_back(Point2D(8,7)); v.push_back(Point2D(5,9));

  vector<Point2D> t(v); // can copy construct a vector

  do { // next_permutation needs Point2D <

    next_permutation(v.begin(), v.end());

    for (size_t s = 0; s < v.size(); ++s) {

      cout << v[s] << " "; // needs Point2D <<

    }

    cout << endl;

  } while (v != t); // needs Point2D == for vector<T> !=

  return 0;

}

The document Chapter - C++ Classes, PPT, C++ Programming, Semester, Engineering - Computer Science Engineering (CSE) is a part of Computer Science Engineering (CSE) category.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)

FAQs on Chapter - C++ Classes, PPT, C++ Programming, Semester, Engineering - Computer Science Engineering (CSE)

1. What are classes in C++?
Ans. Classes in C++ are user-defined data types that contain data members and member functions. They are used to create objects, which are instances of a class. Classes provide a blueprint for creating objects with similar properties and behaviors.
2. What is the role of classes in C++ programming?
Ans. Classes in C++ play a crucial role in object-oriented programming. They encapsulate data and functions into a single unit, allowing for better organization and reusability of code. Classes also enable the concept of inheritance, where one class can inherit properties and behaviors from another class.
3. How do you define a class in C++?
Ans. To define a class in C++, you use the class keyword followed by the class name and a pair of curly braces. Within the braces, you can declare member variables and member functions. Here's an example: ```cpp class MyClass { // Member variables int myVariable; // Member functions void myFunction(); }; ```
4. What is the difference between a class and an object in C++?
Ans. In C++, a class is a blueprint or template for creating objects, while an object is an instance of a class. A class defines the properties and behaviors that objects of that class will have. You can create multiple objects from a single class, each with its own set of values for the class's member variables.
5. How are classes and objects related in C++ programming?
Ans. In C++, classes and objects are closely related. A class defines the structure and behavior of objects, while an object is an instance of a class. You can think of a class as a blueprint and an object as the actual building created from that blueprint. Objects can access the member variables and member functions of their class.
Download as PDF

Top Courses for Computer Science Engineering (CSE)

Related Searches

Extra Questions

,

MCQs

,

study material

,

C++ Programming

,

Chapter - C++ Classes

,

ppt

,

C++ Programming

,

Previous Year Questions with Solutions

,

Important questions

,

PPT

,

Chapter - C++ Classes

,

C++ Programming

,

video lectures

,

mock tests for examination

,

Semester Notes

,

Exam

,

Engineering - Computer Science Engineering (CSE)

,

shortcuts and tricks

,

Semester

,

PPT

,

past year papers

,

Semester

,

Semester

,

Engineering - Computer Science Engineering (CSE)

,

Engineering - Computer Science Engineering (CSE)

,

Sample Paper

,

practice quizzes

,

pdf

,

Free

,

Chapter - C++ Classes

,

Objective type Questions

,

Summary

,

PPT

,

Viva Questions

;