0% found this document useful (0 votes)
57 views23 pages

C++ Maual

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views23 pages

C++ Maual

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Bharati Vidyapeeth College of Engineering

Department of ExTc

CPP and Java Programming


ECL 304

Dr. Kushal D.Badgujar,


Faculty
Department of Extc,
[email protected]
BHARATI VIDYAPEETH COLLEGE OF ENGINEERING
Department of Electronics and Telecommunication Engineering
Class : S.E.-A & B ECL 304- C++ and Java programming
Sem: III
Practicals Plan

P1 Add Two Numbers (with CPP).

P2 Find Largest Number Among Three Numbers (with CPP) .

P3 Create an object of a class and access class attributes (with CPP) .

P4 Create a class for student to get and print details of a student (with CPP).

P5 Demonstrate example of friend function with class (with CPP).

P6 Implementation inheritance (with CPP).

P7 Display addition of number (with Java)

P8 Accept marks from user, if Marks greater than 40, declare the student as “Pass”
else “Fail”” (with Java).

P9 Accept and display the string entered and execute at least 5 different string
functions on it. (with Java) .

P10 Define a class, describe its constructor, overload the Constructors and instantiate
its object (with Java) .

P11 (Extra) Illustrate method of overriding in Java.

P12 (Extra) Implement Multiple Inheritance (with Java) interface


1. Introduction to CPP
Following figures shows different layers for programming languages.

Data can’t be accessed by outside function.

Accessing data members-


student.getdata(name)
Object Function Information
Name
Practical P1: Add Two Numbers (with CPP)

#include <iostream>
using namespace std;

int main() {

int N1, N2, sum;

cout << "Enter two integers: ";


cin >> N1 >> N2;

// sum of two numbers in stored in variable sum Of Two Numbers


sum = N1+ N2;

// prints sum
cout << N1<< " + " << N2<< " = " << sum;

return 0;
}

Explain following questions:


1. What is iostream?
2. What are “using” and “namespace”?
3. What are << and >> meanings in boolean algebra?
4. What is return 0 ?
Practical P2: Find Largest Number Among Three Numbers (with
CPP).

#include <iostream>
using namespace std;

int main()
{
float n1, n2, n3;

cout << "Enter three numbers: ";


cin >> n1 >> n2 >> n3;

if(n1 >= n2 && n1 >= n3)


cout << "Largest number: " << n1;

if(n2 >= n1 && n2 >= n3)


cout << "Largest number: " << n2;

if(n3 >= n1 && n3 >= n2)


cout << "Largest number: " << n3;

return 0;
}

Explain following questions:


1. How to use nested if ? Any example?
2. Explain for and while loop differences with examples?
Program P3: Create an object of a class and access class attributes
(with CPP).
/* C++ program to create an object of a class
and access class attributes */

#include <iostream>
#include <string>
using namespace std;

// class definition
// "student" is a class
class Student {
public: // Access specifier
int rollNo; // Attribute (integer variable)
string stdName; // Attribute (string variable)
float perc; // Attribute (float variable)
};

int main()
{
// object creation
Student std;
// Accessing attributes and setting the values
std.rollNo = 101;
std.stdName = "Million Dinar Baby";
std.perc = 98.20f;
// Printing the values
cout << "Student's Roll No.: " << std.rollNo << "\n";
cout << "Student's Name: " << std.stdName << "\n";
cout << "Student's Percentage: " << std.perc << "\n";

return 0;
}

Explain following questions:


1. How to create objects from class?
2. How to access data members of the object?
3. How to access member functions of the object?
Program P4: Create a class for student to get and print details of a
student (with CPP).

/*C++ program to create class for a student.*/


#include <iostream>
using namespace std;
class student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:
//member function to get student's details
void getDetails(void);
//member function to print student's details
void putDetails(void);
};

//member function definition, outside of the class


void student::getDetails(void){
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter total marks outof 500: ";
cin >> total;
perc=(float)total/500*100;
}
//member function definition, outside of the class
void student::putDetails(void){
cout << "Student details:\n";
cout << "Name:"<< name << ",Roll Number:" << rollNo << ",Total:" << total <<
",Percentage:" << perc;
}
int main()
{
student std; //object creation
std.getDetails();
std.putDetails();
return 0;
}

Explain following questions.


1. What is meaning of void?
2. What is scope resolution operator? Why it is used?
3. What is meaning of public and private members? What are constraints to access
these members?
Program P5: Demonstrate example of friend function with class
(with CPP).

Sometime functions are common to multiple classes. Cpp allows the common function
to be made friendly with multiple classes.
Read following verbatim from Balagurusamy
Explain following questions.
1. How to declare friend function?
2. What are advantages of friend function?
Program P6: Write a program in CPP to implement inheritance.

Description-
Syntax- class derived-class: access-specifier base-class
Access public protected private
Same class yes yes yes
Derived classes yes yes no
Outside classes yes no no

#include <iostream>
using namespace std;

// Base class
class Shape {
protected:
int width;
int height;
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);

// Print the area of the object.


cout << "Total area: " << Rect.getArea() << endl;
return 0;
}

Explain following questions.


1. What is inheritance in CPP?
2. What are types of inheritances in CPP?
3. What are advantages of inheritances?
4. Explain example of any other program with inheritance in CPP.
Program P7: Addition of two numbers using Scanner in Java
In Java, the entry point from where the programs are executed or the entry point of Java
programs is defined by the main() method.

Is it possible to execute java program without main?


Yes->
class StaticBlock {
static
{
System.out.println(
"This class can be executed without
main");
System.exit(0);
}
}
static block in Java represents a group of statements those are executed only once
when the class is loaded into the memory by ClassLoader.

Program P7
import java.util.Scanner;
public class Add2Numbers
{

public static void main(String[] args)


{
int num1, num2, sum;
Scanner input = new Scanner(System.in);
System.out.println("Enter First Number: ");
num1 = input.nextInt();
System.out.println("Enter Second Number: ");
num2 = input.nextInt();
input.close();
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}
Explain the following questions:
1. What is public?
2. What is static?
3. What is void?
4. What is String[] args ?
5. How to read a value from user in Java?
Program P8: Accept marks from user, if Marks greater than 40,
declare the student as “Pass” else “Fail”” in Java.

import java.util.Scanner;

class Main {
public static void main(String[] args) {

// take input from users


Scanner input = new Scanner(System.in);
System.out.println("Enter your marks: ");
double marks = input.nextDouble();

// check the condition


String result = (marks > 40) ? "pass" : "fail";

System.out.println("Your " + result + " the exam.");


input.close();
}
}

Explain the following questions:


1. Modify the program with if conditional statement
2. How to use scanner?
3. What are methods associated with scanner object?
Program P9: Accept and display the string entered and execute at
least 5 different string functions on it (with Java).
Some of string methods in Java -
#1) Java String split()
#2) String contains()
#3) Java String indexOf()
#4) Java String compareTo()
#5) Java String toString()
#6) String charAt()
#7) String reverse()
#8) String to CharArray()
#9) String replace()
#10) Concatenation
#11) Substring Method()
#12) Length

1.Displaying the string and its length


package codes;
import java.lang.String;
public class StringMethods {

public static void main(String[] args) {


String str = "String123";
System.out.println(str.length());
System.out.println(str);
}
}

2.String Concatenation
package codes;
import java.lang.String;
public class StringMethods {
public static void main(String[] args) {
String str1 = "Software";
String str2 = "Testing";
System.out.println(str1 + str2);
System.out.println(str1.concat(str2));
}
}

3. Character array from string


package codes;
import java.lang.String;
public class StringMethods {
public static void main(String[] args) {
String str = "India";
char[] chars = str.toCharArray();
System.out.println(chars);
for (int i= 0; i< chars.length; i++) {
System.out.println(chars[i]);
}
}

4. String function charAt()


package codes;
import java.lang.String;
public class StringMethods {
public static void main(String[] args) {

String str = "java string API";


System.out.println(str.charAt(0));
System.out.println(str.charAt(1));
System.out.println(str.charAt(2));
System.out.println(str.charAt(3));
System.out.println(str.charAt(6));
}
}

5. String compareTo()

package codes;
import java.lang.String;
public class StringMethods {
public static void main(String[] args) {

String strA = "Zeus";


String strB = "Chinese";
String strC = "American";
String strD = "Indian";
System.out.println(strA.compareTo(strB));
System.out.println(strC.compareTo(strD));

}
}

Explain the following questions:


1. How to access string methods? What packages are required?
2. Explain all other string methods.
Program P10: Define a class, describe its constructor, overload the
Constructors and instantiate its object in Java.
Rules for creating a Java Constructor-
1. It must not return a value (not even void).
2. The Constructor has the same name as that of class.
Constructor Overloading in Java-

class Demo{
int value1;
int value2;
/*Demo(){
value1 = 10;
value2 = 20;
System.out.println("Inside 1st Constructor");
}*/
Demo(int a){
value1 = a;
System.out.println("Inside 2nd Constructor");
}
Demo(int a,int b){
value1 = a;
value2 = b;
System.out.println("Inside 3rd Constructor");
}
public void display(){
System.out.println("Value1 === "+value1);
System.out.println("Value2 === "+value2);
}
public static void main(String args[]){
Demo d1 = new Demo();
Demo d2 = new Demo(30);
Demo d3 = new Demo(30,40);
d1.display();
d2.display();
d3.display();
}
}
Explain following questions:
1. What is constructor?
2. What is meaning of constructor overloading?
3. How to instantiate objects in java?
Program P11: Illustrate method of over-riding in Java.
//Java Program to demonstrate the real scenario of Java Method Overriding
//where three classes are overriding the method of a parent class.
//Creating a parent class.
class Bank
{
int getRateOfInterest()
{return 0;}
}
//Creating child classes.
class SBI extends Bank
{
int getRateOfInterest()
{return 8;}
}
class ICICI extends Bank
{
int getRateOfInterest()
{return 7;}
}
class AXIS extends Bank
{
int getRateOfInterest()
{return 9;}
}
//Test class to create objects and call the methods
class Test2
{
public static void main(String args[])
{
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}
}
Explain following questions:
1. What is extend?
2. Why overriding is required?
3. What is system.out?

Program P12: Implement Multiple Inheritance using Java.


Why use inheritance in java?
1. To apply method of Over-riding.
2. To eliminate code redundancy and code reusability.

1. Single Inheritance-
class Creature
{
void eat()
{ System.out.println("Eating.......");
}
}
class Cat extends Creature{
void sound()
{ System.out.println("Myav Myav……….");
}
}
class TestInheritance
{
public static void main(String args[])
{
Cat C1=new Cat();
C1.sound();
C1.eat();

}
}
2.Check following code for multiple inheritance

interface A
{
void msg1();
}

interface B
{
void msg2();
}

public class C implements A,B


{
public void msg1()
{
System.out.println("Message 1 from Class A");
}

public void msg2()


{
System.out.println("Message 2 from Class B");
}
}
class Demo
{
public static void main(String args[])
{
C obj=new C();
obj.msg1();// member function from class A
obj.msg2(); // member function from class B

Explain following questions:

1. What is interface in java? What are advantages?


2. What are different Inheritances?
3. What is impelment?

You might also like