Skip to main content

Basics

Introduction to C++ and differences between C


Learning Objective

  • To know differences between C and C++
  • Demonstrate the capability of programming in C++
  • Apply C++ syntax, New/Delete and Reference operations

Resources









Problem Sets

Complete the following problem sets with in 2 hours

Problem Set A

1. Write a simple ‘Hello, new world!’ program in C and CPP edited, compiled and running on your chosen development platform. Change the program to take a command line argument (or multiple command line arguments) and to say ‘Hello name!’
2. Tabulate all the basic C/C++ types, eg. Char, int, float, double. Don’t forget the unsigned, signed, short, long etc. Write a simple C and C++ program to print out the sizes in bytes of each type on your system.
3. Find out on your system what the largest and smallest values are for the basic types above? Write a program in C and C++ to print out the Limits of each data type described in previous program.

Problem Set B

1. Write a function to print the date, for the structure Date in C and C++.
You need to write the code wherever necessary for the given program in C and C++.
Program to print date in C
#include //Standard Input and Output Library

struct DATE{
int day;
int month;
int year;
};
void showDate(struct DATE d);
void setDate(int d, int m, int year, struct DATE * date);
int main(void){
struct DATE date;
setDate(24,8,2009,&date);
showDate(date);
return 1;
}

void showDate(struct DATE d){
//Write your code here 
}

void setDate(int d, int m, int y, struct DATE * date){
//Write your code here
}
Program to print date in C++
#include//Input/Output stream Library
using namespace std;

struct Date{
int day;
int month;
int year;
void showDate(){
 //Write your code here

}
void setDate(int d, int m, int y){
 //Write your code here
}
};

int main(void){
struct Date date;
date.setDate(24,8,2009);
date.showDate();
system("PAUSE");
return 1;
}
Resources:
2. Write the struct Date(of previous program) in separate namespace in C++ and use the Date structure in main to print the date.

Problem Set C

Write the code in C++ for all the problems given in this problem set.
1. STDIO: Read an integer value N from standard input.
  • Print “Hai” for N times using a for-loop.
Resources:
2. STRINGS: use “string” variable
  • Read your firstName
  • Read your lastName
  • Use “+” string operator and contenate firstName and lastName with “_” in between.
  • Display firstName_lastName on the screen
  • Use length() function of string class and display the length of “firstName_lastName”
  • Access the characters in the string “firstName_lastName” and display one by one separated by a single space
  • Ex: Display “Kishore_Prahallad” as “K i s h o r e _ P r a h a l l a d”
  • Display the characters in reverse order
  • Display as “d a l l a h a r P _ e r o h s i K”
Resources
3. Using new and delete in C++
  1. New and Delete for integer:
  • Read an integer N
  • Create a 1-dimensional dynamic integer array using new[] for N integers
  • Read N integers from the standard input
  • Delete the allocated memory using delete[]
  1. New and Delete for Strings:
  • Read an integer N
  • Create a 1-dimensional dynamic string array using new[] for N strings
  • Read N strings from the standard input
  • Delete the allocated memory using delete[]
  1. New and Delete for 2-dimensional Matrix:
  • Read two integers N and M
  • Create a 2-dimensional dynamic integer array using new[]
  • Read N x M integers from standard input
  • Delete using delete[]
Use separate functions for integer, strings and 2D Matrix.
Resources:

Comments

  1. Very good post. These are real evergreen articles.

    You can use this trick in Blogger to do Syntax Highlighting - http://thecustomizewindows.com/2012/07/syntax-highlighting-in-wordpress-without-plugin/

    ReplyDelete

Post a Comment

Popular posts from this blog

Mini Project - Railway Reservation System

Module 1 Overview Railway Reservation System-(RRS) Project is developed to help students learn and realize capability of C programming language, and appreciate procedural approach of developing a system. RRS helps users reserve/cancel berths, search for trains, check reservation/cancellation history, etc... On the other hand it helps the railway reservation officers to prepare reservation charts, organize train schedules, etc... RRS provides two types of log-ins one is the User log-in for railway customers and the other one is Admin log-in for railway reservation officers. Project is divided into 10 modules of four hours duration each. All the modules will give exactly what to develop in the 4 hour time frame. Since most of the modules are inter-related it is important to keep track of the previous code. After submission it is allowed to make changes in the previous code to suit / integrate well to the system. Before we start the project, we need to make the followin...

Inheritance

Week 1 - M5 : Inheritance Learning Objectives At the end of this module, you will be able to Understand the implementation of inheritance in C++ Override the base class members Access overridden base class members using the scope resolution operator Understand the concept of base class initialization Introduction Inheritance The philosophy behind inheritance is to portray things as they exist in the real world. As inheritance is found in real world, it is important feature of OO programming. Inheritance has many advantages, the most important of them being the resuability of code. Once a class is defined and debugged, it can be used to create new subclasses. The reuse of existing class saves time and effort. The Class from which another class is derived is called the base class . The class which inherits the properties of the base class is called the derived class . Each instance of the derived class includes all the members of the base class. Since...