Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Methods and Functions in Carbon Classes

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Hey Ninjas! Do you know the new programming language developed by Google? 

Carbon is a programming language, also known as Carbon-lang, recently developed by Google in July 2022. It is implemented using C++ language to Cover the Shortcomings in C++.

Today we will discuss Classes, Methods in Carbon Classes, and Functions in Carbon Classes. Let's start without wasting any time.

Introduction

Carbon Language

Carbon Language is an Experimental Successor Language of C++. It is written to Cover the gaps in C++ and also to Enhance fast and Scalable development.

Carbon Language

Carbon language is also memory safe which was not there in C++.

So it is very evident that Carbon has lots of advantages over C++, so why not learn it before anybody else does?

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Classes in Carbon

Classes in Carbon is a user-defined data type, also known as the blueprint of the Object of the class.

Classes in Carbon are somewhat similar to the concept of Classes in C++.

It contains data members and member functions, as we have seen in C++.

Member function in carbon classes is further classified into two types one is Method, and the other is Class Functions.

In this blog, we will see what methods are in Carbon Classes and how it is different from class functions in Carbon Classes, and how to access both of them but before that, let's see how to create a class in Carbon.

Example

Here we will create a Class of name Emails with its data members.

Code

package codingninjas_smaple_email API;

class Email{
    var total_message : i64;
    var promotions_message : i64;
    var social_message : i64;
}

Explanation

  • In the above-written Class declaration, "Email" is the name of the class containing three data members, which are "total_message," "social_message," and "promotions_message" the type of data members has been taken i64, which is similar to long, long int in c++.
     
  • i64 represents an integer data type with 64 bits. Similarly, Carbon has also i32, which represents an integer data type with 32 bits, somewhat similar to the int data type in c++.
     

Now let's move to member functions in carbon classes and start with-class functions in Carbon Classes. 

Class Functions

Class Functions in Carbon Classes are instance-free functions, which means that we are not supposed to make any object of the class access the class functions in Carbon. It is similar to static functions in C++.

Like static functions after the declaration, it can be defined inside or outside the class.

Class functions in Carbon Classes are linked to the class itself, so even if we are not declaring any object for our class still, our class function in Carbon Classes can be used, like class name + "."  + class function name.

It is mostly used to define the constructor for the class.

Let's see this with the help of an Example

Example

Let's extend the class definition of Email, which we have used above.

Implementation

package codingninjas_smaple_email API;

// Class name should start with a capital letter
class Email{
    var total_message : i32;
    var promotions_message : i32;
    var social_message : i32;
 
    // Class function
    fn start() -> Email{
        return {.total_message =100,.promotions_message =40,.social_message =60};
    }
    
    // Class function
    fn spam()-> Email{
        return {.total_message =0,.promotions_message =0,.social_message =0};
    }
}

fn Main() -> i32 {

  /* 
    Start function is used here like a Constructor; for calling it, we only used the name of a class. No object is needed.
  */
  var obj: Email = Email. start();
  Print("total message in email are {0}",obj.total_message);
  
  // Initialized new_obj by calling the class function with an old object.
  var new_obj :Email = obj.spam();
  
  Print("total message in email are {0}",new_obj.total_message);
  return 0;
}

Output

Output

Explanation

  • We need to use the keyword "fn" to declare any functions in Carbon classes. In the code written above, we have created a class function named "start", which is more like a constructor to class Email.
     
  • For function declaration in Carbon, we start with the "fn" keyword and then write the function's name after that return type of the function. Here, our return type is Email.
     
  • We can access our class functions without any obj like we have access to the class function "start," but we can also access the class functions with the help of objects, like we have access to the class function "spam".
     
  • We can see in the output that after calling the spam function with obj, the value of the total message got modified according to the instructions given in the spam function.
     

Note: Instead of writing the class name as return type, in class functions, we can also write "Self".

Methods in Carbon Classes

  • Class methods in Carbon classes operate similarly to class functions in Carbon Classes, but there are some significant differences. 
     
  • The class method in the Carbon Classes declaration differs from the class function in this declaration. We use a parameter "me" inside square brackets before the regular parentheses "()" of parameters.
     
fn class_method [me: class_name](arg1 :i32,arg2 :i32){ }

 

  • Here we can see that we have used "me" as a parameter in square brackets and, after that, normal parameters of the function.
     
  • We are adding this"me" parameter as there is no implicit way to access the data members in class methods in Carbon classes, so when we want to access the data member inside the class methods in Carbon classes, we use the "me" parameter.
     
  • Class methods in carbon classes can also be defined outside or inside the class.
     

Let's see the use of Class methods in carbon classes with the help of the example.

Implementation

package codingninjas_smaple_email API;

// Class name should start with capital letter
class Email{
    var total_message : i32;
    var promotions_message : i32;
    var social_message : i32;
    
    // Class function , return type is of email
    fn start() -> Email{
        return {.total_message =100,.promotions_message =40,.social_message =60};
    }
    
    // Class Method ,used with me parameter,return type is i32
    fn double[me : Self](var d:i32)->i32{
        return me.total_message+d;
    }
}

fn Main() -> i32 {
    
  // Object declaration
  var obj : Email = Email.start();
  
  // Print before double method
  Print("total message in email are {0}",obj.total_message);
 
  // Print after double method
  Print("total message in email are {0}",obj.double(32));
  
  return 0;
}

Output

Output

Explanation

  • We have used the "me" parameter to define class methods in carbon classes almost the same way we defined member functions in carbon classes.
     
  • Class methods in Carbon are called using "." then followed by class method name like in the example above.
     
  • We have called our class method double like "obj. double()" also, we have used the "me" parameter to reference the data members of the class as we did inside the class. Class method “double” accessed the data member "total_message" with the “me” parameter only.
     

Now let's see some frequently asked questions on Carbon language.

Frequently Asked Questions

What are the variables in Carbon?

Whenever we want to store data, it requires a particular memory location. To call or use these stored data, we need to give these locations a variable name.

What does Carbon provide the programmers that make it unique?

Carbon offers programmers modern practices, like generics, modular code organization, and simple syntax. Carbon matches the performance of C++ and alongside provides ease to extending the existing libraries, which is not possible in C++.

Why are data types necessary in Carbon?

Data Types are required for Carbon or any programming language because they help interpreters or compilers understand the type of any value.

Is carbon programming language bi-directional?

Carbon language is Bi-directional as it allows us to call C++ language code from Carbon and also Carbon language code to C++.

How are arrays declared in the Carbon language?

To declare arrays in Carbon language using array types and array size. For example, var arr: [i32; 6] = (1,2,3,4,5,6);

Conclusion

In this article, we learned what objects in Carbon are. We also understood what classes in Carbon are while covering members of a class. We also learned how to declare a class along with the class application.

To learn more about the Carbon programming language, refer to,

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Happy Learning!

Topics covered
1.
Introduction
2.
Carbon Language
3.
Classes in Carbon
3.1.
Example
3.1.1.
Code
3.1.2.
Explanation
4.
Class Functions
4.1.
Example
4.2.
Implementation
4.2.1.
Output
4.3.
Explanation
5.
Methods in Carbon Classes
5.1.
Implementation
5.1.1.
Output
5.2.
Explanation
6.
Frequently Asked Questions
6.1.
What are the variables in Carbon?
6.2.
What does Carbon provide the programmers that make it unique?
6.3.
Why are data types necessary in Carbon?
6.4.
Is carbon programming language bi-directional?
6.5.
How are arrays declared in the Carbon language?
7.
Conclusion