Skip to playerSkip to main contentSkip to footer
  • 7/10/2025
Concrete classes have no pure virtual functions, all data members and methods are completely defined. Concrete classes can be instantiated. https://www.softprayog.in/programming/concrete-classes-in-cpp
Transcript
00:00Hello and welcome to this video. I am Karnesh Jhouri and this video is about
00:05concrete classes in C++. There are concrete classes and there are abstract
00:13classes. As the name suggests concrete and abstract classes are opposites of
00:19each other. The key difference is that concrete classes can be instantiated
00:25whereas abstract classes cannot be instantiated. We can create objects of
00:32concrete classes but not of abstract classes. Concrete classes must not contain
00:39any pure virtual function. Abstract classes must have at least one pure
00:46virtual function. So abstract classes have to be subclassed and the pure virtual
00:53function needs to be implemented in the derived class. Concrete classes can
01:00behave like built-in types. The data members are part of the definition and
01:05objects can be created and initialized like the built-in types. You can have
01:12local variables on the stack for objects, pass objects by value to functions and
01:18copy objects using the assignment operator. And if concrete classes implement
01:24operator overloading functions, you can use operators like plus, minus, star, slash,
01:30like the built-in types. In fact concrete classes do better than the built-in types.
01:37For example, vector is better than array because its size is not fixed at compile
01:43time and error handling is better. Similarly, C++ strings are preferable to
01:50C style strings. We'll have two examples of concrete classes. First, an arithmetic type
01:56complex and second, a container class vector. This is an implementation of complex
02:04complex class. It is a concrete class. There are two data members which are two
02:10doubles a and b and they represent the real and imaginary parts of the complex
02:16number a plus bi where i is equal to square root minus 1. There are three
02:24constructors. The first one takes two arguments, creates an object of class complex
02:30and initializes data members a and b to the arguments h and k.
02:36The second constructor takes one argument, creates an object of class complex, initializes
02:43the real part a to h and the imaginary part b is initialized to 0. Then there is a default
02:50constructor with no arguments. It initializes real and imaginary parts a and b to 0. Now we
02:58have two data members a and b. There should be some way to read and write to these data
03:03members from the users of the class complex. For this, we have getter and setter methods.
03:10The getter methods read and report the values and the setter method sets values. The first
03:18one real constant reads the value of a. It is a getter method and next real double h is a setter
03:27method which sets the value of a. Similarly, we have imac, getter and setter methods and we
03:35have four operator overload functions for operators plus, minus, star for multiplication and slash
03:44for division. Using these, we can add, subtract, multiply and divide complex objects. If we look
03:52at any of the four operator overload functions, they take in one argument. For example, complex operator
04:01plus constant complex reference z constant. There is only one argument z and if we look at its usage,
04:11in the main function, we see something like complex c1 initialized to x plus y. So, how does x plus y happen
04:21when the plus overload function takes in only one argument? The answer to this lies in the fact that
04:27operator overload functions are non-static member functions of class complex. They are called for
04:34objects of class complex. When a non-static member function is executed, an implicit pointer named this
04:42is available which points to the object for which the member function is being executed. So, the first
04:50parameter is the object for which member function is being executed. When you see the statement c1
04:57initialized to x plus y, the expression x plus y needs to be evaluated. The compiler interprets x plus y as
05:06x dot operator plus parenthesis y which means operator overload plus for object x and add object y to it.
05:16So, y is added to x and the result is used for initializing c1. Let us quickly look at the main function.
05:26x and y are two complex objects. x initialized to 2.0 real, 3.0 imaginary and y initialized to 3.3 real and 4.5 imaginary.
05:40Then we have c1, c2, c3 and c4 complex objects initialized to x plus y, x minus y, x star y and x slash y respectively.
05:54And we print c1, c2, c3 and c4. Next, we change the values for x and y and we recalculate and print.
06:04We can compile and run this program and it prints results which are okay.
06:09Vectors. Our second example is vectors. A vector is a container of other objects. A vector stores its
06:18elements sequentially much like an array but it is not fixed size like array. Its size is dynamic.
06:25It can grow or shrink at run time. In c++, vector is normally preferred over arrays and is in fact
06:35most commonly used container in c++. And here is an implementation of vector class.
06:42Looking at the program, first we have the data members.
06:45The size of vector is sg and the type of sg is size t not int. The type size t is unsigned and is
06:56guaranteed to be large enough to hold the size of the largest object on the system. Whereas int is
07:03signed and there is no guarantee about its size. So size t is preferred type for variables that hold
07:11size of something and it makes our program portable. The elements are stored in an array and double
07:17pointer element points to an array of sg doubles. The constructor creates the vector from an initializer
07:25list of double. It creates an array of doubles of the size of the list and sg is also initialized
07:32from the size of the list. The elements are copied in the array. The destructor deletes the elements
07:38and there is the important operator overload function for the square brackets. With this,
07:44we can access the elements of a vector and there is the getter function which gives the size of the
07:49vector. In the main function, we initialize vector v1 with initializer list and we print it in the for loop.
07:56We have looked at concrete classes in this video. You can find all this information
08:03at Soft Pryok website. Here is the QR code and the link is there in the description. Please
08:10subscribe to Soft Pryok, click on the bell icon and enable notifications. Thanks very much for watching.
08:18Take care and stay safe.