Introduction to C++

Introduction to C++


History of C++


  • C++ was developed by Bjarne Stroustrup in 1979 at AT&T Bell Labs.
  • C++ is an object-oriented language which includes imperative as well as generic programming features.
  • C++ was developed with features inherited from SIMULA 67 and ALGOL 68.
  • C++ was initially called “C with classes”.

Following are various applications of C++ language:
  • Systems programming
  • Embedded systems programming
  • Weather control systems
  • Defence systems
  • Web applications
  • Server programming
  • Databases
  • Web search
  • e-commerce

Structure of a C++ Program


A C++ program can contain several parts or sections as shown below:

All sections except the main() function section are optional. Every C++ program must contain a main() function section. Classes in C++ are similar to structures in C. Classes in C++ can contain both variables and functions, where as it is not possible with structures in C. Functions can be defined inside and outside classes in C++.

Include Files Section


A C++ program might include one or more header files using the #include directive. The syntax for including header files is given below:

#include<stdio.h> [or] #include “stdio.h”

The extension of header files in C++ is “.h”. In the above example, the header files stdio.h. is included. All the function declarations in that header file are available in our program. Header files can be included anywhere in the program. Good programming practice is to include them at the beginning of the program.

Class Declaration or Definition Section


One or more classes can be defined in this section. A class can be defined after the main() section. Good programming practice is to define classes before main() section.
A class definition must terminate with a semi-colon (;). A class definition contains variables, function declarations (prototypes), or function definitions.

Class Function Definitions Section

This contains the definitions of functions that were declared in the above section (class declaration section). Functions with less code are defined within the class. Such functions are treated as inline functions. Functions with large amount of code are defined outside the class.

main() Function Section


Every C++ program must contain a main() function as the execution starts from main() function itself. A main() function might contain the code to create and use the objects of classes created in above sections.

Header Files and Libraries in C++


A library provided by a programming language consists of pre-defined functionality arranged in several files. These libraries can be used by programmers to write programs with their own logic without focusing on housekeeping functionality.
C++ library provides several header files (.h files) which includes a lot of pre-defined functionality like reading and writing to files, memory management routines, console I/O routines etc.

Below are some of the header files and the library functions available in those header files in C++:



A Sample C++ Program


Let’s consider the following C++ program which prints “Hello World” on to the console or terminal window. We will look at various elements used in the program.

//C++ program to print "Hello World"
#include <iostream>
using namespace std; 
int main() 
{
cout<<"Hello Netaji";
return 0;
}
Output of the above code is:

Hello  Netaji
In the above program, iostream is the name of a header file which contains I/O functionality. In order to take input and print some output using our C++ program, we have to include that header file using #include directive.

Note that writing “.h” at the end of the header file name results in an error. Such style is deprecated in newer versions of C++.
The cin and cout elements are declared in std namespace inside iostream header file. So, to use either cin or cout, we have to write using namespace <name>. If you don’t want to write this line, you have to write std::cin and std::cout in your program instead of cin and cout.

Every C++ program must contain a main() function which specifies the starting point of execution. Since return type of main is int, you should return an integer value back in your program.

cout is the standard output object. It is used in conjunction with insertion operator (<<) to print output on the screen.
The return statement returned a value 0 which tells the operating system that the program has completed successfully. A non-zero value tells that the program execution was unsuccessful.

Finally, the first line is a comment. We can write single line comments in C++ using // and multi-line comments using /* and */.

Differences between C and C++


Following are various difference between C and C++:





netaji gandi Thursday, July 18, 2019
What is the difference between list and tuples in Python?

Q1. What is the difference between list and tuples in Python?

LIST vs TUPLES

LISTTUPLES
Lists are mutable i.e they can be edited.Tuples are immutable (tuples are lists which can’t be edited).
Lists are slower than tuples.Tuples are faster than list.
Syntax: list_1 = [10, ‘Chelsea’, 20]Syntax: tup_1 = (10, ‘Chelsea’ , 20)

Q2. What are the key features of Python?

  • Python is an interpreted language. That means that, unlike languages like Cand its variants, Python does not need to be compiled before it is run. Other interpreted languages include PHP and Ruby.
  • Python is dynamically typed, this means that you don’t need to state the types of variables when you declare them or anything like that. You can do things like x=111 and then x="I'm a string" without error
  • Python is well suited to object orientated programming in that it allows the definition of classes along with composition and inheritance. Python does not have access specifiers (like C++’s publicprivate).
  • In Python, functions are first-class objects. This means that they can be assigned to variables, returned from other functions and passed into functions. Classes are also first class objects
  • Writing Python code is quick but running it is often slower than compiled languages. Fortunately,Python allows the inclusion of C based extensions so bottlenecks can be optimized away and often are. The numpy package is a good example of this, it’s really quite quick because a lot of the number crunching it does isn’t actually done by Python
  • Python finds use in many spheres – web applications, automation, scientific modeling, big data applications and many more. It’s also often used as “glue” code to get other languages and components to play nice.

netaji gandi
DBMS PL/SQL Programs

DBMS PL/SQL Programs


Q1.sum of 2 nos
DECLARE
                n1 number;
                n2 number;
                sum1 number;
BEGIN
                n1:=&n1;
                n2:=&n2;
                sum1:=n1+n2;
dbms_output.put_line('sum of 2 nos is'||sum1);
end;
/


Q2.greatest of 2 nos
DECLARE
n1 number;
n2 number;
BEGIN
n1:=&n1;
n2:=&n2;
if n1>n2
THEN dbms_output.put_line('greatest of 2 nos is'||n1);
ELSE
dbms_output.put_line('greatest is '||n2);
END IF;
END;
/


Q3.print n numbers
DECLARE
n1 number;
BEGIN
                n1:=&n1;
FOR i in 1..n1
loop
dbms_output.put_line(''||i);
end loop;
end;
/

Q4. Write a program to find the sum of the digits of the number:
DECLARE
N number ;
S NUMBER :=0;
R NUMBER;
begin
n:=&N;
WHILE N<>0 LOOP
R := MOD(N,10);
S := S + R;
N := TRUNC(N/10);
end loop;
dbms_output.put_line('THE SUM OF THE DIGITS = ' || S);
end;
/
Q5. Program to accept a number from user and print number in reverse order.
Program: -
declare
  num1 number(5);
  num2 number(5); 
  rev number(5);
begin
  num1:=&num1;
  rev:=0;
  while num1>0
  loop
    num2:=num1 mod 10;
    rev:=num2+(rev*10);
    num1:=floor(num1/10);
  end loop;
  dbms_output.put_line('Reverse number is: '||rev);
end;
/


Q6. Program to reverse a string.
declare
  str1 varchar2(30);
  len number(3);
  str2 varchar2(30);
  i number(3);
begin
  str1:='&str1';
  len:=length(str1);
  for i in reverse 1..len
   loop
    str2:=str2 || substr(str1,i,1);
   end loop;
  dbms_output.put_line('Reverse string is: '||str2);
end;
/

Q7.program to find a number prime or not
declare
       n number;
        i number;
        counter number;
    begin
        n:=&n;
        i:=1;
        counter:=0;
        if n=1
           then dbms_output.put_line('1 is neither prime nor composite.');
       elsif n=2
           then dbms_output.put_line('2 is even prime');
       else
           for i in 1..n loop
               if mod(n,i)=0
                   then counter:=counter+1;
               end if;
           end loop;
     end if;
       if counter=2
           then dbms_output.put_line(n||' is a prime No.');
       else
           dbms_output.put_line(n||' is a not prime No.');
       end if;
   end;
/
Q8. Program to print sum of first n natural numbers.
Declare
  i number:=0;
  n number;
  sum1 number:=0;
  Begin
  n:=&n;
  while i<n+1
  loop
 sum1:=sum1+i;
  dbms_output.put_line(i);
  i:=i+1;
  end loop;
  dbms_output.put_line('The sum is:'||sum1);
  End;
/
Q9.program to find fabonacci series
DECLARE
    num NUMBER;
    a NUMBER:= 0;
    b NUMBER:= 1;
    c NUMBER;
BEGIN
    num:='&Input_Number';
    DBMS_OUTPUT.PUT_LINE(a);
    DBMS_OUTPUT.PUT_LINE(b);
    FOR i in 3..num LOOP
             c := a + b;
    DBMS_OUTPUT.PUT_LINE(c);
             a:=b;
             b:=c;
    END LOOP;
END;
/                    

Q10.Program to find factorial of a number
DECLARE
    num NUMBER:='&Input_Number';
    i NUMBER;
    f NUMBER:=1;
BEGIN
    FOR i IN 1..num LOOP
        f := f * i;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE(f||' Is Factorial Of '||num);
END;
/

Q11.program to find multiplication table of a given number
DECLARE
    num1 NUMBER;
    i NUMBER;
    BEGIN
    num1:='&INPUT_NUMBER';
    FOR i IN 1..10 LOOP
    DBMS_OUTPUT.PUT_LINE(num1*i);
   EXIT WHEN i=10;
    END LOOP;
   END;
/

Q12.program to find area of the circle
declare
    c number(5);
    r number:='&no';
    a number(5);
    begin
    c:=3.14*r*r;
    dbms_output.put_line('area of circle is'||c);
   end;
/
Q13. program to check whether number is Armstrong or not.
declare
  pnum number(5);
  tot number(5);
  lp number(3);
  tmp number(5);
begin
  pnum:=&pnum;
  tmp:=pnum;
  tot:=0;
  while tmp>0
  loop
    lp:=tmp mod 10;
    tot:= tot + (lp*lp*lp);
    tmp:=floor(tmp/10);
  end loop;
  if(tot like pnum) then
    dbms_output.put_line(pnum||' is armstrong.');
  else
    dbms_output.put_line(pnum||' is not armstrong.');
  end if;
end;
/

Q14.program to find greatest of three numbers
declare
a number;
b number;
c number;
begin
a:=&a;
b:=&b;
c:=&c;
if a=b and b=c and c=a then
dbms_output.put_line('ALL ARE EQUAL');
elsif a>b and a>c then
             dbms_output.put_line('A IS GREATER');
         elsif b>c then
                dbms_output.put_line('B IS GREATER');
  else
                dbms_output.put_line('C IS GREATER');
end if;
end;
/
Q15.program to find palindrome or not
DECLARE
    len NUMBER;
    str VARCHAR2(20) := '&Input_String';
    chkstr VARCHAR2(20);
BEGIN
    len := LENGTH(str);
    FOR i IN REVERSE 1..len LOOP
        chkstr := chkstr||SUBSTR(str,i,1);
    END LOOP;
    IF chkstr = str THEN
        DBMS_OUTPUT.PUT_LINE(str||' Is A Palindrome!');
    ELSE
        DBMS_OUTPUT.PUT_LINE(str||' Is Not A Palindrome!');
    END IF;
END;

Q16.program to generate prime numbers upto n numbers

DECLARE
Num number;
prime integer;
BEGIN
Num:=&num;
    FOR i IN 1..num LOOP
    prime :=1;
    FOR j IN 2..i-1
    LOOP
        IF MOD(i,j)=0 THEN
            prime:=0;
        END IF;
        EXIT WHEN prime=0;
    END LOOP;
    IF prime=1 THEN
        DBMS_OUTPUT.PUT_LINE(i);
    END IF;
    END LOOP;
END;
/

netaji gandi Thursday, July 11, 2019

Java Programming Lab

☕ Java Programming Lab Select a laboratory session to view programs and documentation Week 01 ...