Showing posts with label OOPs. Show all posts
Showing posts with label OOPs. Show all posts

Sep 21, 2020

Destructors in C++ | C++ Language | Coding Winds

                                                            Destructors

 

You all must have heard about constructors, it's uses and properties. If you haven't then I recommend you to first read the article about constructors (link given below)

 

https://codingwinds.blogspot.com/2020/08/constructors-and-destructors-in-c-c.html

 

So, A destructor, as the name suggests , is used to destroy the objects (free the memory space) that have been created by constructor.Just like a constructor, destructor is a member function whose name is same as class name but preceded by a tilde(~) i.e we have to use a tilde before writing a class name to call a destructor.

Now for the betting understand of the concept l, let's talk about It's properties to have a better insight.

 

Properties

-It is called automatically whenever object is destroyed or removed from the memory.

-It is used to perform any operation at the time of destruction of any object.

-It is called as per LIFO(Stack) method.(IMP)

-It cannot have parameters. Thus, it cannot be overloaded.

-It can be virtual.

 

Now let's take an example how the objects are released from the memory.

 

Suppose we have a class whose name is Complex.

Complex c1,c2,c3;

Here c1, c2, c3 are objects of Complex class. So, c3 will be destructed first from the memory, then after c2 will be destructed and finally c1 will be destructed.

 

I hope after reading this article, the topic of destructor will be clear but If you still face some kind of doubt or have any query then you can contact us.

 

Thank you.

Jul 5, 2020

OOPs Concepts in JAVA | Classes | Objects | Methods | Constructors | JAVA Language | Coding Winds

OOPs

OOPs stands for Object Oriented Programming Language and is one of the key concepts in java programming language since it gives us the flexibility of writing lengthy code in a more optimized way. The four basic pillars of OOPs are abstraction, encapsulation, inheritance and polymorphism. Basically it consists of Classes, Methods, Objects and Constructers, which are some very important things in java. So let's talk about them.

CLASSES

As discussed earlier, classes are the blocks within which our code can be written or methods can be created. And the same methods can be called in the main method of our program. Remember there can be only one main method in a program. So let's begin with looking at the syntax of the class.

Syntax:

class className {

}

So a class is basically written with a keyword 'class' followed by the class name, and all the required methods along with their body can be written inside the class accordingly. Below is the basic example of class inside which is a simple program which add and prints two (already initialized) variables.

Ex:

public class CodingWinds {

    

public static void main(String[] args) {

         int a=3,b=10;

         int c=a+b;

         System.out.println(c);

     }

}

 

METHODS

Methods (which are known as function in C and C++) are actually some code written inside a pair of brackets, which can be called anywhere throughout our program , to execute the lines of code it contains. It must be written inside a class. Let's look at the basic syntax of a method.

Syntax:

returnType methodName () {

}

So a method name contains the return type (the type of value it returns, it can be 'void' if it returns nothing) followed by the name of the method then followed by a pair of round brackets and then the body of the method within a pair of curly braces (after the round brackets). In some cases, parameter list is also passed inside the methods (while calling them), parameters list is the list of data values (along with their type) that can be used inside the method body. So the syntax of the methods with parameters list can be seen below

ReturnType methodname (parameter-list){

}

One or more parameters can be passed inside the methods. If they are more than one ,they must be separated by commas. Below is the example of methods passing two parameters.

Ex:

static void sum(int num1, int num2) {

int result =num1+num2;

System.out.println(result);

}

Note: Here the keyword ‘static’ here means that the method belongs to the same class from which it is being called.

Calling a Method

Methods can be called by two ways in java, first they can be called directly into the program and second, they can be called by creating objects. Since, we have not discussed about the objects yet, let’s see the first way.

Syntax:

methodName();

or

methodName(parameter-list);

As you can see we can simply call the method by writing their name followed by the round brackets. If you need to pass some parameters they can be written inside the round brackets (separated by a comma), but while calling the methods we do not need to write the type before the parameters .

Ex:

import java.util.*;

 

public class Coding_Winds {

static void sum(int num1, int num2) {

int result =num1+num2;

System.out.println(result);

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

sum(2,10);        //Calling the methods

 

}

}

Note that if we need to pass a string value as a parameter then we need to warp that inside double quotes (during method calling), and same is happened when we pass char value but in the case of char we wrap our parameter within a single quote.

Ex:

import java.util.*;

 

 

public class HackerRanck2 {

     static void xyz(String a, char b){

     System.out.println("your entered string is \""+a+"\"");

     System.out.println("your entered character is\'"+b+"\'");

     }

public static void main(String[] args) {

         xyz("hello", 'a');

     }

}


Output


OBJECTS

Objects are the separated memory blocks which a user can create in a program to use them to collect values for the variables present inside the class, for which an object is being created. We can also use the objects to call methods inside the class. So below is the syntax of how can we create an object inside java.

Syntax:

ClassName objectName = new ClassName();

Ex:

Coding_Winds str  = new Coding_Winds();

 

Now the object ‘str’ can be used to store values inside any variable present inside the class (from which object has been created), and even to call any methods inside that class. Below example shows us how can we store values inside the class using variable and call the methods using the same.

Ex:

import java.util.*;

 

public class Coding_Winds {

String x;

void welcome() {

 

System.out.println(x);

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

Coding_Winds str  = new Coding_Winds ();

str.x= "Welcome to Coding Winds";

str.welcome();

 

}

}

CONSTRUCTORS

There are two ways to initialize the value of variables inside a class, first by using the dot operator with the object, second by using Constructors. Constructors can be considered as another kind of methods in java, but unlike methods they don’t return a value that is the first thing. Second their name is same to the class in which they are present, lest they are also allowed to pass parameters through them. Below is the syntax of the constructors in java.

Syntax:

constructorName () { 

}

Once again remember that a constructor’s name is always same as the name of the classes in which they are present. Below is the example of the constructor

Ex:

import java.util.*;

 

class Hello {

     String x;

      Hello (){

           x= "Welcome to Coding winds";

          }      

}

public class Coding_Winds {

    

public static void main(String[] args) {

         Scanner sc = new Scanner(System.in);

      Hello str  = new Hello();

      System.out.println(str.x);

    

       

     }

}

 In the above example a constructor is created and the string inside the class ‘Hello’ is printed in the main method, as the variable ‘x’ is already initialised inside the class, we will get an output as soon as we print the variable ‘x’. In the next example we have created an another constructor so that the user can also pass the value for the variable ‘x’ and that certain value will be printed, but even if the user will not pass the value a default value will always be there and will get printed if we’ll print it in the main method.

Ex:

package advancProblems;

import java.util.*;

 

class Hello {

     String x;

      Hello (){

           x= "Welcome to Coding winds";

          }

      Hello(String y) {

          x=y;

      }

          

}

public class HackerRanck2 {

    

public static void main(String[] args) {

         Scanner sc = new Scanner(System.in);

      Hello str  = new Hello("Hello Guys!");

      System.out.println(str.x);

     }

}

 

Output

As seen above, now we have passed a value that has beem printed on the console, but even if the user doesn’t passes the value this time, there will always be a default value (“Welcome to Coding Winds”) that will be get printed if we print the variable ‘x’ .


Hope you are clear on this  topic do read our more articles on JAVA LANGUAGE.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


This article is SUBMITTED By : Pranjal Rai


Do subscribe to our daily blog update by clicking here.


Thank You!