Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs,, Study notes of Computer Science

MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs,

Typology: Study notes

2021/2022

Uploaded on 01/13/2023

akash-kumar-116
akash-kumar-116 🇮🇳

3 documents

Partial preview of the text

Download MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, MCA Docs, and more Study notes Computer Science in PDF only on Docsity! Home - CodeWithHarry 1/15 Basics Basic syntax and functions from the C++ programming language. Boilerplate #include <iostream> using namespace std; int main() { cout << "Welcome To CodeWithHarry"; return 0; } cout << It prints output on the screen cout << "This is C++ Programming"; cin >> It takes input from the user cin >> variable_name Data types The data type is the type of data Character type Typically a single octet(one byte). It is an integer type char variable_name; Integer type The most natural size of integer for the machine Home - CodeWithHarry 2/15 int variable_name; Float type A single-precision floating-point value float variable_name; Double type A double-precision floating-point value double variable_name; Void type Represents the absence of the type void Boolean type bool Escape Sequences It is a sequence of characters starting with a backslash, and it doesn't represent itself when used inside string literal. Alarm or Beep It produces a beep sound \a Backspace It adds a backspace Home - CodeWithHarry 5/15 It is a collection of characters surrounded by double quotes Declaring String // Include the string library #include <string> // String variable string variable1 = "Hello World"; append function It is used to concatenate two strings string firstName = "Harry "; string lastName = "Bhai"; string fullName = firstName.append(lastName); cout << fullName; length function It returns the length of the string string variable1 = "CodeWithHarry"; cout << "The length of the string is: " << variable1.length(); Accessing and changing string characters string variable1 = "Hello World"; variable1[1] = 'i'; cout << variable1; Maths C++ provides some built-in math functions that help the programmer to perform mathematical operations efficiently. max function It returns the larger value among the two Home - CodeWithHarry 6/15 cout << max(25, 140); min function It returns the smaller value among the two cout << min(55, 50); sqrt function It returns the square root of a supplied number #include <cmath> cout << sqrt(144); ceil function It returns the value of x rounded up to its nearest integer ceil(x) floor function It returns the value of x rounded down to its nearest integer floor(x) pow function It returns the value of x to the power of y pow(x, y) Decision Making Instructions Conditional statements are used to perform operations based on some condition. If Statement Home - CodeWithHarry 7/15 if (condition) { // This block of code will get executed, if the condition is True } If-else Statement if (condition) { // If condition is True then this block will get executed } else { // If condition is False then this block will get executed } if else-if Statement if (condition) { // Statements; } else if (condition){ // Statements; } else{ // Statements } Ternary Operator It is shorthand of an if-else statement. variable = (condition) ? expressionTrue : expressionFalse; Switch Case Statement It allows a variable to be tested for equality against a list of values (cases). switch (expression) { case constant-expression: statement1; statement2; Home - CodeWithHarry 10/15 return_type function_name(data_type parameter...){ //code to be executed } Function Call function_name(arguments); Recursion Recursion is when a function calls a copy of itself to work on a minor problem. And the function that calls itself is known as the Recursive function. void recurse() { ... .. ... recurse(); ... .. ... } Object-Oriented Programming It is a programming approach that primarily focuses on using objects and classes. The objects can be any real-world entities. class class Class_name { public: // Access specifier // fields // functions // blocks }; object Class_name ObjectName; Constructors Home - CodeWithHarry 11/15 It is a special method that is called automatically as soon as the object is created. class className { // The class public: // Access specifier className() { // Constructor cout << "Code With Harry"; } }; int main() { className obj_name; return 0; } Encapsulation Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user. #include<iostream> using namespace std; class ExampleEncap{ private: /* Since we have marked these data members private, * any entity outside this class cannot access these * data members directly, they have to use getter and * setter functions. */ int num; char ch; public: /* Getter functions to get the value of data members. * Since these functions are public, they can be accessed * outside the class, thus provide the access to data members * through them */ int getNum() const { return num; } char getCh() const { return ch; Home - CodeWithHarry 12/15 } /* Setter functions, they are called for assigning the values * to the private data members. */ void setNum(int num) { this->num = num; } void setCh(char ch) { this->ch = ch; } }; int main(){ ExampleEncap obj; obj.setNum(100); obj.setCh('A'); cout<<obj.getNum()<<endl; cout<<obj.getCh()<<endl; return 0; } File Handling File handling refers to reading or writing data from files. C provides some functions that allow us to manipulate data in the files. Creating and writing to a text file #include <iostream> #include <fstream> using namespace std; int main() { // Create and open a text file ofstream MyFile("filename.txt"); // Write to the file MyFile << "File Handling in C++"; // Close the file MyFile.close(); }
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved